Questions tagged [sorting]

Sorting is the process of applying some order to a collection of items.

Sorting is the process of applying some order to a collection of items. There are various for doing this, including , , and .

From the Sorting Algorithms Wikipedia entry:

In a is an algorithm that puts elements of a list in a certain order. The most-used orders are order and order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for data and for producing human-readable . More formally, the output must satisfy two conditions:

  1. The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order);
  2. The output is a permutation (reordering) of the input.

Related tags:

67842 questions
849
votes
19 answers

How do you sort a dictionary by value?

I often have to sort a dictionary, consisting of keys & values, by value. For example, I have a hash of words and respective frequencies, that I want to order by frequency. There is a SortedList which is good for a single value (say frequency), that…
Kalid
  • 20,388
  • 14
  • 40
  • 45
809
votes
40 answers

Sorting object property by values

If I have a JavaScript object such as: var list = { "you": 100, "me": 75, "foo": 116, "bar": 15 }; Is there a way to sort the properties based on value? So that I end up with list = { "bar": 15, "me": 75, "you": 100, "foo":…
Steerpike
  • 15,129
  • 7
  • 37
  • 53
755
votes
36 answers

Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM

I have a computer with 1 MB of RAM and no other local storage. I must use it to accept 1 million 8-digit decimal numbers over a TCP connection, sort them, and then send the sorted list out over another TCP connection. The list of numbers may…
Favourite Onwuemene
  • 3,847
  • 8
  • 25
  • 42
750
votes
11 answers

How to sort a list/tuple of lists/tuples by the element at a given index?

I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5,6], [7,8,9]] data = [(1,2,3), (4,5,6), (7,8,9)] And I want to sort by the 2nd element in the subset. Meaning, sorting by 2,5,8 where 2 is from (1,2,3),…
Stan
  • 32,761
  • 45
  • 112
  • 170
634
votes
32 answers

Sort JavaScript object by key

I need to sort JavaScript objects by key. Hence the following: { 'b' : 'asdsad', 'c' : 'masdas', 'a' : 'dsfdsfsdf' } Would become: { 'a' : 'dsfdsfsdf', 'b' : 'asdsad', 'c' : 'masdas' }
vdh_ant
  • 10,716
  • 10
  • 62
  • 81
566
votes
20 answers

Sort array of objects by object fields

How can I sort this array of objects by one of its fields, like name or count ? Array ( [0] => stdClass Object ( [ID] => 1 [name] => Mary Jane [count] => 420 ) [1] => stdClass Object …
Alex
  • 60,472
  • 154
  • 401
  • 592
565
votes
18 answers

Swift how to sort array of custom objects by property value

lets say we have a custom class named imageFile and this class contains two properties. class imageFile { var fileName = String() var fileID = Int() } lots of them stored in Array var images : Array = [] var aImage =…
mohacs
  • 12,002
  • 9
  • 47
  • 76
555
votes
12 answers

Why is there no SortedList in Java?

In Java there are the SortedSet and SortedMap interfaces. Both belong to the Java Collections framework and provide a sorted way to access the elements. However, in my understanding there is no SortedList in Java. You can use…
Jijoy
  • 10,877
  • 12
  • 38
  • 48
554
votes
6 answers

Sort a list by multiple attributes?

I have a list of lists: [[12, 'tall', 'blue', 1], [2, 'short', 'red', 9], [4, 'tall', 'blue', 13]] If I wanted to sort by one element, say the tall/short element, I could do it via s = sorted(s, key = itemgetter(1)). If I wanted to sort by both…
headache
  • 6,719
  • 5
  • 16
  • 6
506
votes
20 answers

How to sort an array of associative arrays by value of a given key in PHP?

Given this array: $inventory = array( array("type"=>"fruit", "price"=>3.50), array("type"=>"milk", "price"=>2.90), array("type"=>"pork", "price"=>5.43), ); I would like to sort $inventory's elements by price to get: $inventory = array( …
Matt
  • 19,783
  • 24
  • 71
  • 113
464
votes
9 answers

List OrderBy Alphabetical Order

I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List. For the sake of this example, let's say I have a List of a Person type with a property of lastname. How would I sort this List using a lambda expression? List
SaaS Developer
  • 9,525
  • 7
  • 31
  • 45
457
votes
16 answers

Sorting list based on values from another list

I have a list of strings like this: X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] Y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1 ] What is the shortest way of sorting X using values from Y to get the following output? ["a", "d", "h", "b", "c",…
Legend
  • 104,480
  • 109
  • 255
  • 385
445
votes
21 answers

How can I convert the "arguments" object to an array in JavaScript?

The arguments object in JavaScript is an odd wart—it acts just like an array in most situations, but it's not actually an array object. Since it's really something else entirely, it doesn't have the useful functions from Array.prototype like…
Andrew Coleson
  • 9,452
  • 7
  • 28
  • 30
443
votes
11 answers

How to sort a list of strings?

What is the best way of creating an alphabetically sorted list in Python?
skolima
  • 29,517
  • 26
  • 108
  • 146
405
votes
23 answers

Fastest sort of fixed length 6 int array

Answering to another Stack Overflow question (this one) I stumbled upon an interesting sub-problem. What is the fastest way to sort an array of 6 integers? As the question is very low level: we can't assume libraries are available (and the call…
kriss
  • 21,366
  • 15
  • 89
  • 109