Skip to content
lsl.devlsl.devLSL Dev

llListSort

function
Function syntax
list llListSort(
  1. list src,// List to be sorted.
  2. integer stride,// Number of entries per block/stride (defaults to 1 if less than 1).
  3. integer ascending// Boolean. If TRUE, sorts in ascending order; if FALSE, sorts in descending order.
);
Returns
list
Energy
10

Returns a copy of the list src, sorted into blocks of stride in ascending order (if ascending is TRUE) or descending order (if FALSE). Only works if the first entry of each block shares the same datatype.

Returns a list that is src sorted by stride.

How to use
list result = llListSort([], 0, 0);

The sort order is affected by type. For strings and keys, it is case sensitive and sorts by Unicode character code.

llListSort(["a", "á", "B", "C", "d", "e"], 1, TRUE) // returns ["B", "C", "a", "d", "e", "á"]

For ascending sort, each type is sorted individually and then feathered to have the same order of types.

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"]
llListSort(["2ae", "ah5", "1ag", "aa6", "3ac", "ad7", "ab8", "4af", "ai9"], 1, TRUE);
// returns ["1ag", "2ae" ,"3ac" ,"4af" ,"aa6" ,"ab8" ,"ad7" ,"ah5" ,"ai9"]

As a reminder, although floats and integers are both numeric and comparable, they too are treated separately.

llListSort([1, 3, 5, 2.0, 4.0, 6.0], 1, TRUE) // returns the list unchanged
  • It uses an unoptimized selection sort algorithm, which is an algorithm with a Big O of N². A JIRA issue exists to improve this function, SVC-2988.

  • Originally the wiki stated that non-zero values for the “ascending” parameter would produce an ascending sort. That was incorrect. For this function, the value must be exactly 1 (or TRUE) for an ascending sort.

  • Vectors are sorted by magnitude. SVC-5643

  • Rotations are not sorted. An ascending sort will simply leave any rotations unchanged, and a descending sort will reverse their order.

  • For special float values: having NaNs in a list can confuse the sort order, as they are uncomparable. Negative zeroes are retained, but their positions with regard to ordinary zeroes depend on the list. Infinities behave as expected.

  • For descending sort, if there are mixed types, the final order is deterministic (the same input will always produce the same output) but it can be completely useless.

    llListSort([2, "B", "C", 3, 1, "A"], 1, FALSE) // returns ["A", 3, 1, "C", "B", 2]

    If there are no mixed types, however, the descending sort works just fine.

  • When the stride is greater than 1, if the list length is not a multiple of the stride, the list will be returned unchanged.

  • When strings contain numbers, the numbers are still sorted left-to-right like any other character, which may not necessarily match numeric order:

    llListSort(["127", "3", "25"], 1, TRUE) // returns ["127", "25", "3"] because the 1 in 127 is before the 2 in 25 which is before the 3

    To sort them in numeric order, numbers in strings can be padded with zeros:

    llListSort(["127", "003", "025"], 1, TRUE) // returns ["003", "025", "127"]
  • This order differs from the order of items in a prim’s inventory, which is “natural order” (e.g “New Script 2” is sorted before “New Script 11”).

Example 1
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
}
}

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.

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));

This returns in chat:

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

The same ordered in descending order returns even more meaningless results:

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, FALSE);
llSay(0, llList2CSV(tmplist));

returns in chat:

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

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.)

llListSort(myList, 1, TRUE); // You've wasted cpu time; you didn't capture the results
list newlist = llListSort(myList, 1, TRUE);// Okay. You've captured the results.
llSay(0,llList2CSV(llListSort(myList, 1, TRUE))); // No need to capture, using the results right away.

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.)

Setting the parameter “integer ascending” to TRUE returns a sorted list that is in ascending order.

For example: [“Apples”, “Bananas”, “Oranges”]

Setting the parameter “integer ascending” to FALSE returns a sorted list that is in descending order.

For example: [“Oranges”, “Bananas”, “Apples”]

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.

list demographics = ["John Adams", "male", "2007-06-22", "Shirley Bassey", "female", "2005-11-02", "Matt Damon", "male", "2008-05-19"];
list tmplist_1 = llListSort(demographics, 1, TRUE);
//tmplist_1 == ["2005-11-02", "2007-06-22", "2008-05-19", "John Adams", "Matt Damon", "Shirley Bassey", "female", "male", "male"]
//The strides have been destroyed, the sorted data is now useless

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

list tmplist_2 = llListSort(demographics, 3, TRUE);
//templist_2 = ["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.

From the issue templates included by the wiki article:

  • SVC-2988 (nf): Convert llListSort() to use faster sorting methods!
  • SVC-5146 (nf): llSortedListFindList() - improved llListFindList() for known to be sorted lists
Original wiki source

Some wiki templates and tables need their original context. View this article on the Second Life Wiki. Technical wording and examples are retained from the source; historical guidance may differ from current behavior.