Difference between revisions of "LlListSort"

From Second Life Wiki
Jump to: navigation, search
m
(added usage notes)
Line 6: Line 6:
 
|p3_type=integer|p3_name=ascending|p3_desc=if {{LSLG|FALSE}} then the sort order is descending, otherwise the order is ascending.
 
|p3_type=integer|p3_name=ascending|p3_desc=if {{LSLG|FALSE}} then the sort order is descending, otherwise the order is ascending.
 
|func_footnote
 
|func_footnote
|func_desc
+
|func_desc=This function supports [[List#Strided_lists|Strided Lists]].
 
|return_text=that is '''src''' sorted by '''stride'''.
 
|return_text=that is '''src''' sorted by '''stride'''.
 
|spec=A slow bubble sort is employed to perform the sort.<br/>
 
|spec=A slow bubble sort is employed to perform the sort.<br/>
Line 37: Line 37:
 
|also_tests
 
|also_tests
 
|also_articles
 
|also_articles
|notes
+
|notes=
 +
'''Data Types'''
 +
 
 +
llListSort really only works on items of the same type. It will work on lists that hold diverse data types -- to be clear, it won't blow up your script -- but the results returned are usually meaningless.
 +
 
 +
<lsl>
 +
list mylist = ["brown", <0.000000, 0.000000, 0.000000>, "house", 17.005, 100, "cat", <3.000000, 3.000000, 3.000000>, 39];<br />
 +
list tmplist = llListSort(mylist,1,TRUE);<br />
 +
llSay(0, llList2CSV(tmplist));<br />
 +
</lsl>
 +
 
 +
This returns in chat:
 +
 
 +
brown, <0.000000, 0.000000, 0.000000>, cat, 17.004999, 39, house, <3.000000, 3.000000, 3.000000>, 100
 +
 
 +
 
 +
 
 +
 
 +
'''Using the Results'''
 +
 
 +
It's important to note that the source list that you are sorting will remain unchanged. Instead, a new, sorted list will be produced. So, it's important that you capture this with a variable (unless you are acting directly on the results.
 +
 
 +
<lsl>
 +
llSortList(myList,1,TRUE); // you've sorted for nothing; you didn't capture the results<br />
 +
list newlist = llSortList(myList,1,TRUE);//okay. You've captured the results.<br />
 +
llSay(0,list2CSV(llSortList(myList,1,TRUE))); //no need to capture, using the results right away.
 +
</lsl>
 +
 
 +
 
 +
'''"Stride" parameter'''
 +
 
 +
Most times, you will want to set "integer stride" to 1 (0 also works) to tell it to sort each item in the list on its own basis. (If you are working with a strided list, though, see the special section below on sorting strides.)
 +
 
 +
 
 +
'''Sort Order'''
 +
 
 +
Setting the parameter "integer ascending" to TRUE gives you back a sort that is in ascending order. E.g.:
 +
 
 +
Apples<br />
 +
Bananas<br />
 +
Citrus<br />
 +
 
 +
 
 +
Setting the parameter "integer ascending" to FALSE gives you back a sort that is in descending order. E.g.:
 +
 
 +
Citrus<br />
 +
Bananas<br />
 +
Apples<br />
 +
 
 +
 
 +
'''Sorting Strided Lists'''
 +
 
 +
If you have a [[List#Strided_lists|strided list]], in which you are keeping related pieces of data together in chunks, letting each list element sort on its own basis would be disastrous.
 +
 
 +
Sample List:
 +
 
 +
list demographics = ["John Adams", "male", "2007-06-22", "Shirley Bassey", "female", "2005-11-02", "Matt Damon", "male", "2008-05-19"];<br />
 +
 
 +
Example 1:
 +
 
 +
list tmplist = llListSort(demographics,01,TRUE);<br />
 +
llSay(0, llList2CSV(tmplist));<br />
 +
 
 +
This gives you:
 +
 
 +
2005-11-02, 2007-06-22, 2008-05-19, John Adams, Matt Damon, Shirley Bassey, female, male, male
 +
 
 +
Which destroys your data collection.
 +
 
 +
 
 +
Example 2:
 +
 
 +
Instead, because you have the data grouped (aka "strided") in sets of 3, you need to do this:
 +
 
 +
list tmplist = llListSort(demographics,3,TRUE);<br />
 +
llSay(0, llList2CSV(tmplist));<br />
 +
 
 +
This gives us the correct sort.
 +
 
 +
John Adams, male, 2007-06-22, Matt Damon, male, 2008-05-19, Shirley Bassey, female, 2005-11-02
 +
 
 +
 
 +
When storing data in strided lists, it's often worth it down the road to take a moment at the outset to think about how you are most likely to want to sort them, if ever the need arose. Remember, you can only sort on the first element in each group of elements. If you think you're mostly likely to want to sort on gender (to use the above list example), you should make gender the first element in the data grouping.
 +
 
 +
 
 
|permission
 
|permission
 
|negative_index
 
|negative_index

Revision as of 12:04, 13 July 2008

Summary

Function: list llListSort( list src, integer stride, integer ascending );

This function supports Strided Lists.
Returns a list that is src sorted by stride.

• list src List to be sorted.
• integer stride number of entries per stride, if less then 1 it is assumed to be 1.
• integer ascending if FALSE then the sort order is descending, otherwise the order is ascending.

Specification

A slow bubble sort is employed to perform the sort.
The sort order is affected by type.
Each type is sorted individually and then feathered to have the same order of types. <lsl>llListSort([1, "C", 3, "A", 2, "B"], 1, TRUE) returns [1, "A", 2, "B", 3, "C"] llListSort([1, 3, 2, "C", "A", "B"], 1, TRUE) returns [1, 2, 3, "A", "B", "C"]

llListSort([1, "C", 3, "A", 2, "B"], 2, TRUE) returns [1, "C", 2, "B", 3, "A"]</lsl>

Caveats

A bubble sort is a sorting algorithm with a Big O of N*N. Why Linden Labs did not use a N*log(N) sorting algorithm such as Heap Sort or Merge Sort is unkown...

All Issues ~ Search JIRA for related Bugs

Examples

<lsl> list numbers = [3, "three", 2, "two", 1, "one"]; default {

   state_entry()
   {
       llOwnerSay(llDumpList2String(numbers, ","));
       // Object: 3,three,2,two,1,one
       numbers = llListSort(numbers, 2, TRUE);
       llOwnerSay(llDumpList2String(numbers, ","));
       // Object: 1,one,2,two,3,three
   }

}

</lsl>

Notes

Data Types

llListSort really only works on items of the same type. It will work on lists that hold diverse data types -- to be clear, it won't blow up your script -- but the results returned are usually meaningless.

<lsl> list mylist = ["brown", <0.000000, 0.000000, 0.000000>, "house", 17.005, 100, "cat", <3.000000, 3.000000, 3.000000>, 39];
list tmplist = llListSort(mylist,1,TRUE);
llSay(0, llList2CSV(tmplist));
</lsl>

This returns in chat:

brown, <0.000000, 0.000000, 0.000000>, cat, 17.004999, 39, house, <3.000000, 3.000000, 3.000000>, 100



Using the Results

It's important to note that the source list that you are sorting will remain unchanged. Instead, a new, sorted list will be produced. So, it's important that you capture this with a variable (unless you are acting directly on the results.

<lsl> llSortList(myList,1,TRUE); // you've sorted for nothing; you didn't capture the results
list newlist = llSortList(myList,1,TRUE);//okay. You've captured the results.
llSay(0,list2CSV(llSortList(myList,1,TRUE))); //no need to capture, using the results right away. </lsl>


"Stride" parameter

Most times, you will want to set "integer stride" to 1 (0 also works) to tell it to sort each item in the list on its own basis. (If you are working with a strided list, though, see the special section below on sorting strides.)


Sort Order

Setting the parameter "integer ascending" to TRUE gives you back a sort that is in ascending order. E.g.:

Apples
Bananas
Citrus


Setting the parameter "integer ascending" to FALSE gives you back a sort that is in descending order. E.g.:

Citrus
Bananas
Apples


Sorting Strided Lists

If you have a strided list, in which you are keeping related pieces of data together in chunks, letting each list element sort on its own basis would be disastrous.

Sample List:

list demographics = ["John Adams", "male", "2007-06-22", "Shirley Bassey", "female", "2005-11-02", "Matt Damon", "male", "2008-05-19"];

Example 1:

list tmplist = llListSort(demographics,01,TRUE);
llSay(0, llList2CSV(tmplist));

This gives you:

2005-11-02, 2007-06-22, 2008-05-19, John Adams, Matt Damon, Shirley Bassey, female, male, male

Which destroys your data collection.


Example 2:

Instead, because you have the data grouped (aka "strided") in sets of 3, you need to do this:

list tmplist = llListSort(demographics,3,TRUE);
llSay(0, llList2CSV(tmplist));

This gives us the correct sort.

John Adams, male, 2007-06-22, Matt Damon, male, 2008-05-19, Shirley Bassey, female, 2005-11-02


When storing data in strided lists, it's often worth it down the road to take a moment at the outset to think about how you are most likely to want to sort them, if ever the need arose. Remember, you can only sort on the first element in each group of elements. If you think you're mostly likely to want to sort on gender (to use the above list example), you should make gender the first element in the data grouping.

Deep Notes

Search JIRA for related Issues

Source

lsa_bubble_sort(): 'linden\indra\lscript\lscript_alloc.h'

Signature

function list llListSort( list src, integer stride, integer ascending );