LessonsIntermediate: Stacks, objects and dataLesson 30

Sorting lists

Put lines and items in order: alphabetically, by number or date, backwards, and by any part of each line you choose.

  • Intermediate
  • Lesson 30 of 83
  • About 9 minutes
  • 8 examples

Sorting a list

A list in xTalk is just text: items separated by commas, or lines. sort puts them in order, in place. Text is sorted alphabetically unless you say otherwise:

ExampleRun it in the playground
put "pear,apple,fig,banana" into tFruit
sort items of tFruit
put tFruit
sort items of tFruit descending
put tFruit
Output
apple,banana,fig,pear
pear,fig,banana,apple

sort tList on its own sorts lines, so sort lines of tList and sort tList are the same.

Numbers need numeric

Sorted as text, 10 comes before 9, because the character 1 comes before 9, just as apple comes before banana. Say numeric to sort by value:

ExampleRun it in the playground
put "10,9,100,2" into tNumbers
sort items of tNumbers
put tNumbers
sort items of tNumbers numeric
put tNumbers
sort items of tNumbers numeric descending
put tNumbers
Output
10,100,2,9
2,9,10,100
100,10,9,2

Dates and times have a sort of their own, dateTime, which understands every format xTalk can read (Dates and times):

ExampleRun it in the playground
put "3/1/2024,1/15/2023,12/25/2023" into tDates
sort items of tDates dateTime
put tDates
Output
1/15/2023,12/25/2023,3/1/2024

Sorting by part of each line

The real power is by: sort on any expression, where each stands for the line or item being placed. To sort people by their age, the second item of each line:

ExampleRun it in the playground
put "Ada,36" & return & "Alan,41" & return & "Grace,85" & return & "Linus,29" into tPeople
sort lines of tPeople numeric by item 2 of each
put tPeople
Output
Linus,29
Ada,36
Alan,41
Grace,85

The expression can be anything at all, even a function of your own:

ExampleRun it in the playground
function vowelCount pWord
   put 0 into tCount
   repeat for each char tChar in pWord
      if tChar is among the chars of "aeiou" then add 1 to tCount
   end repeat
   return tCount
end vowelCount

put "rhythm,banana,cat,queue" into tWords
sort items of tWords numeric by vowelCount(each)
put tWords
sort items of tWords numeric by the length of each
put tWords
Output
rhythm,cat,banana,queue
cat,queue,rhythm,banana

Look at the last line: rhythm and banana both have six letters, and they kept the order the first sort gave them. That is no accident, as the next section explains.

Sorting by two things

xTalk's sort is stable: lines that compare equal keep the order they had. So to sort by two keys, sort by the less important one first, then by the more important one. Here, by age, and people of the same age by name:

ExampleRun it in the playground
put "Zoe,30" & return & "Sam,25" & return & "Amy,30" & return & "Ben,25" into tPeople
sort lines of tPeople by item 1 of each
sort lines of tPeople numeric by item 2 of each
put tPeople
Output
Ben,25
Sam,25
Amy,30
Zoe,30

Capitals and accents

Text sorts by the order of the characters in Unicode, which puts every capital before every small letter when caseSensitive is true, and puts accented letters after z. Ordinary sorting ignores case. For words in a human language, international sorts the way a dictionary of the user's language would:

ExampleRun it in the playground
put "zebra,Éclair,apple,eagle" into tWords
sort items of tWords
put tWords
sort items of tWords international
put tWords
Output
apple,eagle,zebra,Éclair
apple,eagle,Éclair,zebra

Try it yourself

Here are the scores of a game, a line each: name,points. Sort them into a leaderboard, highest score first, and put the winner's name on a line of its own after the board.

Show a solution
ExampleRun it in the playground
put "Kim,340" & return & "Raj,1200" & return & "Lee,875" & return & "Ana,95" into tScores
sort lines of tScores numeric descending by item 2 of each
put tScores
put "Winner:" && item 1 of line 1 of tScores
Output
Raj,1200
Lee,875
Kim,340
Ana,95
Winner: Raj

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.