6 Replies Latest reply on Jun 21, 2007 1:35 PM by Applied CD

# Sort list items according to predifined order

hi all,

I wonder is there any way to sort the items of a list according to a predefined order (e.g., 2,3,1)?

Many thanks
• ###### 1. Re: Sort list items according to predifined order
Hi wgb14,

you can sort lists in all sorts of ways, but what are the rules of your
ordening?

Manno

wgb14 wrote:
> hi all,
>
> I wonder is there any way to sort the items of a list according to a predefined order (e.g., 2,3,1)?
>
> Many thanks

--
----------
Manno Bult
http://www.aloft.nl
• ###### 2. Re: Sort list items according to predifined order
As far as I know the only predefined sort order is alphabetic. You could write a function using list.addAt(position,value) to add new items in the proper location and maintain the sort order yourself. This might involve a lot of looping through the target list, depends on how complex your sort rules are.

I had a project where I needed to keep a list of file names sorted by rules other than alphabetic. I actually used a big lookup table (case statement) to append letters to the beginning of each name just before adding them to the list, the letters were stripped off when data was retrieved from the list. This trick allowed me to use list.sort() to maintain the sort order. It’s an ugly solution and I’m not proud of it, but it got the job done.
• ###### 3. Re: Sort list items according to predifined order
I want to sort the list according to the position of the items. For example if the list returns the items in 1,2,3, I want it to be 2,3,1. Is there any xtra that i can do that?
• ###### 4. Re: Sort list items according to predifined order
You certainly don’t need an xtra, a simple sort handler will do, but unless I'm missing something I’m still not sure what your sort rules are. For example, if your list contains strings, how should the following list be sorted?

[“red”, “green”, “blue”]

If your list contains integers, how do decide to go from [1, 2, 3] to [2, 3, 1]? If your rule is to move all items up one and roll the first item to the end, that’s easy … but I’m just guessing at your rule from the example.
• ###### 5. Sort list items according to predifined order
OK, lets say that my rule is what you are saying "If your rule is to move all items up one and roll the first item to the end" how do you do that using lingo? My list contains strings!
• ###### 6. Re: Sort list items according to predifined order
OK, here it is. Please keep in mind that this rule is particularly easy because the order of items in the list doesn’t change very much, you could have real world rules that are much more complex.

the following example would call the attached handler to change the list order:
tList = ["red","green","blue"]
tList = shuffleForward(tList)