Erlang Central

Difference between revisions of "String Sort List"

From ErlangCentral Wiki

m
(Solution)
Line 22: Line 22:
  
 
[[Category:CookBook]][[Category:StringRecipes]][[Category:ListRecipes]]
 
[[Category:CookBook]][[Category:StringRecipes]][[Category:ListRecipes]]
 
 
 
[http://www.casinos-go.com/online-casino-tips/free-online-casino-slot.html free online casino slot]
 
[http://www.gambling-online-theory.com/casinos/casinos-strategy.html casinos strategy]
 
[http://www.casino-games-wiki.com/index.php/casino_games_online casino games online]
 
[http://www.slots-wiki.com/index.php/slots_tips slots tips]
 
[http://www.gambling-online-theory.com/online-casino/online-casino-for-cash.html online casino for cash]
 
[http://www.casinos-new.com/poker.html Online casino - Poker in casino]
 
[http://www.3wcasinos.com/roulette-tips/roulette-description.html roulette description]
 
[http://www.gambling-online-theory.com/fortune-roulette/on-line-roulette-game.html on line roulette game]
 
[http://www.casino-theory.com/online-casino-bonus/online-casino-net.html online casino net]
 
[http://www.magical-casino.com/casino_risk.html Gambling Online - Risks.]
 

Revision as of 12:54, 23 November 2006

Sorting a list of strings

Problem

You want to sort a list of strings.

Solution

Use the lists:sort operation to sort the lists. Note that by default sorting is case-sensitive. However, since you can use the lists:sort/2 variant of the function to supply your own sorting function this can be modified.

1> lists:sort(["foo", "bar", "baz", "qux"]).
["bar","baz","foo","qux"]
2> lists:sort(fun(A, B) -> A =< B end, ["foo", "bar", "baz", "qux"]).
["bar","baz","foo","qux"]
3> lists:sort(fun(A, B) -> A =< B end, ["foo", "bar", "baz", "qux"]).
["bar","baz","foo","qux"]
4> lists:sort(fun(A, B) -> A >= B end, ["foo", "bar", "baz", "qux"]).
["qux","foo","baz","bar"]

The sort operation uses the merge-sort algorithm, which takes time O(n log n) where n is the length of the list.