20.8. Sorting, reversing and rotating lists

| Chapter 20: Lists |

| 20.8. Sorting, reversing and rotating lists

Any list L can be reversed:

reverse (list of values)

This phrase puts the list in reverse order. 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. Example: let L be {11, 12, 14, 15, 16, 17}; reverse L; results in L being {17, 16, 15, 14, 12, 11}.

And any list can similarly be sorted:

sort (list of values)

This phrase puts the list into ascending order. Example: let L be {6 PM, 11:13 AM, 4:21 PM, 9:01 AM}; sort L; results in L being {9:01 AM, 11:13 AM, 4:21 PM, 6 PM}.

sort (list of values) in reverse order

This phrase puts the list into descending order. Example: let L be {6 PM, 11:13 AM, 4:21 PM, 9:01 AM}; sort L in reverse order; results in L being {6 PM, 4:21 PM, 11:13 AM, 9:01 AM}.

sort (list of values) in random order

This phrase puts the list into a uniformly random order, shuffling it as if it were a pack of cards. Example: let L be {1, 2, 3, 4, 5, 6}; sort L in random order; might result in L being {3, 1, 5, 6, 4, 2}. Or any of 719 other arrangements, including being left as it was.

Lists of objects can also be sorted in property value order. For instance,

sort (list of objects) in (property) order

This phrase puts the list into ascending order of the values of the given property for the items in the list; this is only allowed if all of those values do have the property in question. Example: let L be the list of people; sort L in carrying capacity order; would arrange people with weaklings first, titans last.

sort (list of objects) in reverse (property) order

This phrase puts the list into descending order of the values of the given property for the items in the list; this is only allowed if all of those values do have the property in question. Example: let L be the list of people; sort L in reverse carrying capacity order; would arrange people with titans first, weaklings last.

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 (list of values)

This phrase shuffles the entries of the list forwards (to the right) by one place, so that the 1st becomes 2nd, the 2nd becomes 3rd, and so on until the last, which becomes the new first entry. Example: let L be { "cow", "heifer", "bullock" }; rotate L; results in L being { "bullock", "cow", "heifer" }.

rotate (list of values) backwards

This phrase shuffles the entries of the list backwards (to the left) by one place, so that the 3rd becomes 2nd, the 2nd becomes 1st, and so on; the previous 1st entry becomes the new last entry. Example: let L be { "cow", "heifer", "bullock" }; rotate L backwards; results in L being { "heifer", "bullock", "cow"}. (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