Questions tagged [sorting]

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

Sorting is the process of arranging a collection of items into a sequence by applying an ordering. 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:

76323 questions
4170
votes
38 answers

How can I pair socks from a pile efficiently?

Yesterday I was pairing the socks from the clean laundry and figured out the way I was doing it is not very efficient. I was doing a naive search — picking one sock and "iterating" the pile in order to find its pair. This requires iterating over n/2…
amit
  • 175,853
  • 27
  • 231
  • 333
4048
votes
60 answers

Sort array of objects by string property value

I have an array of JavaScript objects: var objs = [ { first_nom: 'Lazslo', last_nom: 'Jamf' }, { first_nom: 'Pig', last_nom: 'Bodine' }, { first_nom: 'Pirate', last_nom: 'Prentice' } ]; How can I sort them by the value of…
Tyrone Slothrop
  • 40,787
  • 3
  • 17
  • 8
3414
votes
34 answers

How do I sort a dictionary by value?

I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary. I can sort on the keys, but how can I sort based on the values? Note: I have read…
Gern Blanston
  • 42,482
  • 19
  • 50
  • 64
2739
votes
22 answers

How to sort a list of dictionaries by a value of the dictionary in Python?

How do I sort a list of dictionaries by a specific key's value? Given: [{'name': 'Homer', 'age': 39}, {'name': 'Bart', 'age': 10}] When sorted by name, it should become: [{'name': 'Bart', 'age': 10}, {'name': 'Homer', 'age': 39}]
masi
  • 118
  • 3
  • 5
  • 9
1872
votes
65 answers

Sort a Map by values

I need to sort a Map on the values. Since the values are not unique, I find myself converting the keySet into an array, and sorting that array through array sort with a custom comparator that sorts on the value associated with the…
Abe
  • 2,463
  • 4
  • 19
  • 16
1683
votes
34 answers

Sorting an array of objects by property values

I've got the following objects using AJAX and stored them in an array: var homes = [ { "h_id": "3", "city": "Dallas", "state": "TX", "zip": "75201", "price": "162500" }, { "h_id": "4", …
TomHankers
1602
votes
24 answers

How to Sort a List by a property in the object

I have a class called Order which has properties such as OrderId, OrderDate, Quantity, and Total. I have a list of this Order class: List objListOrder = new List(); GetOrderList(objListOrder); // fill list of orders I want to sort the…
Shyju
  • 214,206
  • 104
  • 411
  • 497
1483
votes
19 answers

Sort (order) data frame rows by multiple columns

I want to sort a data frame by multiple columns. For example, with the data frame below I would like to sort by column 'z' (descending) then by column 'b' (ascending): dd <- data.frame(b = factor(c("Hi", "Med", "Hi", "Low"), levels = c("Low",…
Christopher DuBois
  • 42,350
  • 23
  • 71
  • 93
1464
votes
32 answers

How do I sort a dictionary by key?

How do I sort a dictionary by its keys? Example input: {2:3, 1:89, 4:5, 3:0} Desired output: {1:89, 2:3, 3:0, 4:5}
Antony
  • 15,257
  • 4
  • 17
  • 18
1379
votes
17 answers

How to Sort a Multi-dimensional Array by Value

How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be. Array ( [0] => Array ( [hashtag] => a7e87329b5eab8578f4f1098a152d6f4 [title] =>…
stef
  • 26,771
  • 31
  • 105
  • 143
1368
votes
32 answers

How to sort an array of integers correctly

Trying to get the highest and lowest value from an array that I know will contain only integers seems to be harder than I thought. var numArray = [140000, 104, 99]; numArray = numArray.sort(); console.log(numArray) I'd expect this to show 99,…
peirix
  • 36,512
  • 23
  • 96
  • 126
1321
votes
27 answers

How do I sort an NSMutableArray with custom objects in it?

What I want to do seems pretty simple, but I can't find any answers on the web. I have an NSMutableArray of objects, and let's say they are 'Person' objects. I want to sort the NSMutableArray by Person.birthDate which is an NSDate. I think it has…
rustyshelf
  • 44,963
  • 37
  • 98
  • 104
1279
votes
29 answers

Sort ArrayList of custom Objects by property

I read about sorting ArrayLists using a Comparator but in all of the examples people used compareTo which according to some research is a method for Strings. I wanted to sort an ArrayList of custom objects by one of their properties: a Date…
Samuel
  • 18,286
  • 18
  • 52
  • 88
1194
votes
9 answers

How do I sort a list of objects based on an attribute of the objects?

I have a list of Python objects that I want to sort by a specific attribute of each object: [Tag(name="toe", count=10), Tag(name="leg", count=2), ...] How do I sort the list by .count in descending order?
Nick Sergeant
  • 35,843
  • 12
  • 36
  • 44
991
votes
45 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
  • 17,163
  • 8
  • 39
  • 53
1
2 3
99 100