Page 1 of 1

(=*.*=) ascending and descending

Posted: Fri May 29, 2015 12:14 pm
by Mariasole
I'm trying to sort a list. It sounds simple but I can not get what I want! :cry:


Here is the list in disorder:

82837598374982756389/0/
92837598374982756389/7/
92837598374982756389/3/
92837598374982756389/2/
82837598374982756389/5/
92837598374982756389/25/

I would like this sorted list as follows:

92837598374982756389/25/
92837598374982756389/7/
92837598374982756389/3/
92837598374982756389/2/
82837598374982756389/5/
82837598374982756389/0/

I wish the first and second elements sorted in descending order! (at the same time)

I could just do this:

Code: Select all

   set the itemDelimiter to numToChar(47) 
   sort lines of field "sort" descending numeric by item 1 of each & item 24 of each

Here's my pigcode :oops: :

Code: Select all

   set the itemDelimiter to numToChar(47) 
   sort lines of field "sort" descending numeric by item 1 of each & descending  numeric item 24 of each
Sorry if trouble for something so stupid :oops: , I read first the pages of forum and the legendary lists.runrev.com :mrgreen: , but there's nothing that I understand :shock: .
Thank you all!

(=*.*=) Mariasole

Re: (=*.*=) ascending and descending

Posted: Fri May 29, 2015 2:00 pm
by dunbarx
Hi.

I assume that "/" ASCII 47?

If so, you want to sort by the second item, not the 24th. Item 2 would be "0" in the first line of your unsorted list. The nice thing is that it does not matter what the length of each item is, that is the point of the item delimiter.

Craig Newman

Re: (=*.*=) ascending and descending

Posted: Fri May 29, 2015 2:50 pm
by Lagi Pittas
Hi,

That still wouldn't work Craig because Mariasole wants the /25/ at the top but it would be under the /7/ and the /3/.
putting the word numeric doesn't fix it.

Padding with zeroes does the trick though.

Code: Select all

   set the itemdel to "/"
   sort lines of field "fldsort"   descending  by  item 1 of each & format( "%03d",item 2 of each ) 
Regards Lagi

Re: (=*.*=) ascending and descending

Posted: Fri May 29, 2015 2:56 pm
by dunbarx
Lagi

Not sure what you mean. Take the raw data in a field. Make another field. Make a button. Put this in the button script:

Code: Select all

on mouseUp
   put "" into fld 2
   get fld 1
   set the itemDel to "/"
   sort it numeric descending by item 2 of each -- or sort it numeric descending by item 2 of each & item 1 of each
   put it into fld 2
end mouseUp
Craig

Re: (=*.*=) ascending and descending

Posted: Fri May 29, 2015 3:06 pm
by Lagi Pittas
Hi Craig

I just did that and it gives me the below with both options you gave

92837598374982756389/25/
92837598374982756389/7/
82837598374982756389/5/ <---- Problem here the 828 is above the 928
92837598374982756389/3/
92837598374982756389/2/
82837598374982756389/0/

using my line above I get

92837598374982756389/25/
92837598374982756389/7/
92837598374982756389/3/
92837598374982756389/2/
82837598374982756389/5/
82837598374982756389/0/

Regards Lagi

Re: (=*.*=) ascending and descending

Posted: Fri May 29, 2015 3:31 pm
by dunbarx
Hmmm.

If you have two sort protocols, one must take precedence over the other. LC supports what is known as a "stable" sort, that is, if one sort is run, the sorted list will remain sorted when a second sort is run.

But only one can be the primary sort criteria. Watch what happens when you try both of these:

Code: Select all

 sort it numeric descending by item 2 of each & item 1 of each
 sort it numeric descending by item 1 of each & item 2 of each
If you think about it, you cannot have it both ways, that is, that both items are independently sorted within the same list. It would break the link between items within lines. What could that look like?

Craig

Re: (=*.*=) ascending and descending

Posted: Fri May 29, 2015 4:34 pm
by jacque
Just run two independent sorts instead of a single compound one.

Code: Select all

   get fld 1
   set the itemDel to "/"
   sort it numeric descending by item 2 of each
   sort it numeric descending by item 1 of each

Re: (=*.*=) ascending and descending

Posted: Fri May 29, 2015 5:52 pm
by dunbarx
Jacque.

This:

Code: Select all

  sort it numeric descending by item 2 of each & item 1 of each
is the same as this:

Code: Select all

sort it numeric descending by item 1 of each 
   sort it numeric descending by item 2 of each
It's that "stable sort" thingie. Maybe it is easier to read the exploded one, but this was an old HC trick, and is still undocumented in LC.

Craig

Re: (=*.*=) ascending and descending

Posted: Fri May 29, 2015 5:56 pm
by jacque
Next time I'll actually try it. ;) I confess I don't much trust undocumented things though.

Re: (=*.*=) ascending and descending

Posted: Fri May 29, 2015 6:32 pm
by dunbarx
Jacque.

It was undocumented in HC as well. I cannot remember where or how I found it, but I think I was the one that did. But I certainly did not create it. Anyway, it at least has a venerable history, and has always worked for me. There is no limit to the number of levels that you can apply to such a one-liner, which work forward in precedence, as you can see from the comparison of the one-line vs. multi-line versions.

Craig

Re: (=*.*=) ascending and descending

Posted: Tue Jun 02, 2015 10:53 am
by Mariasole
Thank you all very much for your help! :P
Thanks to the large community of LiveCode for the attention to us "absolute beginners"! :D
Thanks to Craig, Lagi and the HyperActive Jacqueline! :!: :!: :!:

Thank you all!

=^..^= Mariasole