Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How qsort a str variable?
#1
a="John
Paul
Marcus
Ariel"


sorted a?
#2
The qsort function is not very easy to use but it is universal.

Example:
Code:
Copy      Help
str a=
;John
;Paul
;Marcus
;Ariel


ARRAY(str) b=a
qsort &b[0] b.len sizeof(str) &sortstrproc
a=b
out a

Function sortstrproc:
Code:
Copy      Help
function[c] str&s1 str&s2
int i=q_stricmp(s1 s2)
if(i>0) ret 1
if(i<0) ret -1
#3
Thanks a lot.
#4
and sort integer variables?
#5
Example:
Code:
Copy      Help
ARRAY(int) a.create(4)
a[0]=7
a[1]=3
a[2]=1
a[3]=5
qsort &a[0] a.len sizeof(int) &sortintproc
int i
for i 0 a.len
,out a[i]

Function sortintproc:
Code:
Copy      Help
function[c] &i1 &i2

if(i1>i2) ret 1
if(i1<i2) ret -1
#6
Perfect.


Forum Jump:


Users browsing this thread: 1 Guest(s)