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
4011
votes
37 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
  • 166,614
  • 24
  • 210
  • 314
3418
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
  • 41,734
  • 19
  • 47
  • 64
3234
votes
52 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
  • 32,631
  • 3
  • 14
  • 8
2202
votes
17 answers

How do I sort a list of dictionaries by a value of the dictionary?

I have a list of dictionaries and want each item to be sorted by a specific value. Take into consideration the list: [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] When sorted by name, it should become: [{'name':'Bart', 'age':10},…
masi
  • 118
  • 3
  • 5
  • 9
1737
votes
59 answers

Sort a Map by values

I am relatively new to Java, and often find that 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…
Abe
  • 2,463
  • 4
  • 19
  • 16
1468
votes
31 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
1396
votes
19 answers

How to sort a dataframe by multiple column(s)

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
  • 38,442
  • 23
  • 68
  • 91
1388
votes
22 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 Now I want to sort…
Shyju
  • 197,032
  • 96
  • 389
  • 477
1290
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,085
  • 36
  • 96
  • 104
1245
votes
13 answers

How to Sort 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
  • 23,866
  • 28
  • 96
  • 140
1207
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
  • 17,035
  • 16
  • 45
  • 83
1109
votes
28 answers

How can I sort a dictionary by key?

What would be a nice way to go from {2:3, 1:89, 4:5, 3:0} to {1:89, 2:3, 3:0, 4:5}? I checked some posts but they all use the "sorted" operator that returns tuples.
Antony
  • 11,587
  • 4
  • 15
  • 18
1020
votes
29 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
  • 32,055
  • 22
  • 90
  • 125
956
votes
9 answers

Swift Beta performance: sorting arrays

I was implementing an algorithm in Swift Beta and noticed that the performance was very poor. After digging deeper I realized that one of the bottlenecks was something as simple as sorting arrays. The relevant part is here: let n = 1000000 var x = …
Jukka Suomela
  • 11,423
  • 4
  • 32
  • 45
942
votes
8 answers

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

I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. The list looks like: >>> ut [, , , , , , ...] Each object has…
Nick Sergeant
  • 29,184
  • 12
  • 34
  • 44
1
2 3
99 100