![]() | Chapter 20: Lists | ![]() ![]() |
20.7. Sorting, reversing and rotating lists |
Any list L can be reversed:
reverse L;
Reversing L means that the old entry 1 becomes the new last entry, and so on: reversing an empty list or a list containing only one entry leaves it unchanged.
And any list can similarly be sorted:
sort L;
sort L in reverse order;
sort L in random order;
As with table columns, sorting puts numbers in increasing numerical order, puts texts in increasing alphabetical order and so on.
Lists of objects can also be sorted in property value order. For instance,
sort L in carrying capacity order;
sort L in reverse carrying capacity order;
sorts a list L in ascending, or descending, order of the number of items the entry objects are able to carry.
Rotating a list means moving all of its entries along by one place, and then moving the one on the end back to the start. For instance, if L is {1, 2, 3, 4}, then
rotate L;
produces {2, 3, 4, 1}, whereas
rotate L backwards;
produces {4, 1, 2, 3}. (This achieves the same effect as
reverse L; rotate L; reverse L;
but is a little faster, and a lot less effort to read.)
Previous | Contents | Next |