Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort Array by number from string
#1
Hi everybody

I tried to follow http://www.quickmacros.com/forum/showthr...p?tid=6283 to sort my Array by number - I would prefer Descending (highest first)
I will make an array with products in column 0 and the number value in column 1 that I want to order by

My array looks like:
Product1 -> 95
Product2 -> 112

I wish it would look like:
Product2 -> 112
Product1 -> 95



My macro is:
Code:
Copy      Help
ARRAY(str) am.create(2 2)
int i j
am[0 0]="Product1"
am[0 1]="95"
am[1 0]="Product2"
am[1 1]="112"
str d
am.sort(8 sortwordHits &d )

for i 0 am.len(1)
    for j 0 am.len(2)
        out am[i j]

My 'sortwordHits' function to sort by the number is
Code:
Copy      Help
function# param str&a str&b
&a=&a+(3*sizeof(a))
&b=&b+(3*sizeof(b))
ret StrCompare(b a)


thanks everybody for this great community


I have been using Quick Macro for many many years, it truely is #1 for task automation
#2
If possible, don't use 2D arrays. Use only when the number of columns is unknown.

Macro Macro21
Code:
Copy      Help
out
type PRODUCTN str'product int'n
ARRAY(PRODUCTN) a.create(3)
a[0].product="Product1"
a[0].n=95
a[1].product="Product2"
a[1].n=112
a[2].product="Product3"
a[2].n=5

a.sort(1 sub.sortwordHits)

int n
for n 0 a.len
,out F"product={a[n].product}, n={a[n].n}"


#sub sortwordHits
function# param PRODUCTN&a PRODUCTN&b
if(a.n<b.n) ret -1
if(a.n>b.n) ret 1
#3
That is a great solution - you saved my day!
I have never thought of using an array like that

thank you very much Gintaras!

By the way, why do you start the macro with 'out' ?

Code:
Copy      Help
out

What is the purpose to write a single 'out' without any variables afterwards?
I normally use 'out' for debugging with a variable - ex 'out username' but you write a single 'out' without anything afterwards

I have been wanting to ask for a while Smile
#4
out by itself clears qm output window

Tip
to see function help and tips click the function word in the qm editor window and look at the qm qm status bar window for the info or click the word and click F1
#5
Thanks Kevin!
That's a small but useful feature when debugging.


Forum Jump:


Users browsing this thread: 1 Guest(s)