Need help with sorting numerically

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Need help with sorting numerically

Post by PoLyGLoT » Sun Jun 30, 2013 7:47 pm

Code: Select all

1,3,1,1,1,1
2,3,,,1
3
4,3,,1,1,1
5,3,1,1,,1
6,3,,1,,1
7,3,1,1,1,1
8,3,,1
9,3,,1,1,1
10
11,4,1,1,1,1
12,4,1,1,1,1
13,4,,1,1,1
14,4,,1,1
15,4,,1,1,1
16,4,1
17
18,4,,,1,1
19,4,,,1,1
20,4,,1,1
21,5,,1,1,1
22,5,1
23,5,1,1,1,1
24,5,,,1,1
25
26,5,,,,1
27,5,,,1,1
28,5,,,1,1
29,5,,,,1
30,5,,,1
31,6,1,1,1,1
32,6,1,,1,1
33,6,,1
34,6,1,1,1
35,6,,,,1
36,6,,1,,1
37,6,,1,1,1
38,6,,1,1
39,6,,,1
40,6,,,1,1
41,7,,1,1,1
42,7,1,1,1,1
43,7,1,1,1,1
44,7,,1,,1
45,7,,1,1,1
46,7,1,1
47,7,,,,1
48,7,,1
49,7,,1,,1
50,7,,,1
51,1,1,1,1,1
52,1,,1,1,1
53,1,,,1,1
54,1,,1,,1
55,1,,,,1
56,1,,1,1
57,1,,,,1
58,1,,,,1
59
60,1,,,1,1
61,2,,1,1,1
62
63,2,,,,1
64,2,,,1,1
65,2,,1,1,1
66,2,,,,1
67,2,,,1
68
69
70,2,,1,1,1
Hi all,

I'm trying to sort the above list by item 2 numerically. However, I need each line which does not have an item 2 to stay within it's proper group. Ideally, I want it to start with item 2 = 1, then 2, then 3, and so on, but it should be the case that for each different number 2 set there should be 10 lines (i.e., 10 lines for when item 2 = 1, 10 lines for when item 2 = 2, etc.). Therefore, the blank lines need to stay with their appropriate group.

Another possibility would be to find a way to put in the missing number 2 item. I will tinker with that.

But is it possible to sort them in such a way to accomplish this?

Thanks,
PoLy

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Re: Need help with sorting numerically

Post by SparkOut » Sun Jun 30, 2013 9:10 pm

I am not sure, but I don't think you could use the built in sorts to do this without rebuilding the list line by line. Fortunately "repeat for each..." is a very efficient way of iteration. I think I have understood your problem, I hope this matches what you expect:

Code: Select all

   put field 1 into tData --pick your own source, of course
   repeat for each line tLine in tData
      if item 2 of tLine is empty then
         put "0," before tLine 
      end if
      put tLine & cr after tNewData
   end repeat
   delete the last char of tNewData
   put empty into tData
   sort lines of tNewData ascending numeric by item 1 of each
   sort lines of tNewData ascending numeric by item 2 of each
   repeat for each line tLine in tNewData
      if item 1of tLine is 0 then
         put item 2 to -1 of tLine & cr after tData
      else
         put tLine & cr after tData
      end if
   end repeat
   put tData into field 1
In the event that there is no item 2 for the sort, then it pushes a zero to be the first item, and the numeric index in item one gets moved to item 2. Then a sort by item 1 will make sure that the "blanks" are at the top of the "group" and a subsequent sort by item 2 will put the groups all in order. Then you can rebuild the list again, stripping out the "fake" items as you go.
I'm not sure if it's the most efficient way of doing it, but if I've understood correctly, I think it will do what you need, and should be pretty fast overall.
HTH

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: Need help with sorting numerically

Post by PoLyGLoT » Sun Jun 30, 2013 10:16 pm

Thank you for posting your code.

I swear - as soon as I post a question, I find a solution. I found a slightly easier way to do it (which you could not possibly have known about given that you don't know what I'm doing). Anyways, I figured out a way to simply have no blank item 2's, so that the sort function works properly.

Out of curiosity, I'll probably tinker with your code later. :D

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Need help with sorting numerically

Post by bn » Mon Jul 01, 2013 12:21 am

Hi PoLyGlot,

glad you solved your problem.

since sorting is important and at times confusing nevertheless I post this solution
Assuming the list you posted is the sorted version you can restore that sort order of a scrambled version of your posted list with this

Code: Select all

on mouseUp
   sort field 4 numeric by item 2 of each
   sort field 4 numeric by item 1 of each
end mouseUp
of course field 4 is just my field.

To scramble the field

Code: Select all

on mouseUp
   sort field 4 by random(the number of lines of field 4)
end mouseUp
another approach when sorting is to use a custom sort function. In your case it is not necessary but with a custom function you can solve (almost) any sort problem. Look in the user contributed notes for sort in the dictionary.

Kind regards
Bernd

Post Reply