Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
4180 votes
38 answers
438k views

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 ...
amit's user avatar
  • 176k
4086 votes
61 answers
3.2m views

Sort array of objects by string property value

I have an array of JavaScript objects: var objs = [ { first_nom: 'Laszlo', last_nom: 'Jamf' }, { first_nom: 'Pig', last_nom: 'Bodine' }, { first_nom: 'Pirate', last_nom: '...
Tyrone Slothrop's user avatar
3414 votes
34 answers
5.2m views

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 ...
Gern Blanston's user avatar
2757 votes
22 answers
1.3m views

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}, ...
1879 votes
65 answers
1.7m views

Sort a Map<Key, Value> by values

I need to sort a Map<Key, Value> 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 ...
1692 votes
34 answers
1.5m views

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"...
1618 votes
24 answers
1.8m views

How to Sort a List<T> 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<Order> objListOrder = new List<Order>(); ...
Shyju's user avatar
  • 215k
1491 votes
19 answers
1.4m views

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(...
Christopher DuBois's user avatar
1484 votes
33 answers
2.2m views

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's user avatar
  • 15.5k
1389 votes
17 answers
1.1m views

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 ( [...
stef's user avatar
  • 27k
1383 votes
32 answers
1.2m views

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(); ...
peirix's user avatar
  • 36.8k
1324 votes
27 answers
485k views

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 ...
rustyshelf's user avatar
1282 votes
29 answers
1.3m views

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 ...
Samuel's user avatar
  • 18.3k
1210 votes
9 answers
927k views

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 ....
Nick Sergeant's user avatar
1006 votes
45 answers
1.5m views

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 = { "...
Steerpike's user avatar
  • 17.3k
985 votes
9 answers
119k views

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 ...
Jukka Suomela's user avatar
947 votes
21 answers
809k views

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 ...
Kalid's user avatar
  • 22.3k
903 votes
9 answers
1.3m views

SQL multiple column ordering

How can I sort multiple columns in SQL and in different directions? For instance, 'column1' would be sorted descendingly and 'column2' ascendingly.
Señor Reginold Francis's user avatar
898 votes
11 answers
932k views

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. ...
Stan's user avatar
  • 37.4k
837 votes
38 answers
982k views

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's user avatar
  • 12.8k
761 votes
36 answers
214k views

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 ...
696 votes
8 answers
564k views

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 ...
headache's user avatar
  • 8,457
661 votes
23 answers
531k views

Sort array of objects by one property

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 [...
Alex's user avatar
  • 66.8k
649 votes
20 answers
498k views

How to sort an array of custom objects by property value in Swift

Let's 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 are stored in an Array: ...
modusCell's user avatar
  • 13.2k
642 votes
14 answers
1.2m views

How to sort pandas dataframe by one column

I have a dataframe like this: 0 1 2 0 354.7 April 4.0 1 55.4 August 8.0 2 176.5 December 12.0 3 95.5 February 2.0 4 85.6 January 1.0 5 ...
Sachila Ranawaka's user avatar
625 votes
20 answers
596k views

Sorting list according to corresponding values from a parallel list [duplicate]

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 ...
Legend's user avatar
  • 114k
622 votes
13 answers
647k views

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 ...
Jijoy's user avatar
  • 12.4k
603 votes
23 answers
743k views

Sort an array of associative arrays by column value

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 ...
Matt's user avatar
  • 22.3k
509 votes
16 answers
516k views

Sorting arrays in NumPy by column

How do I sort a NumPy array by its nth column? For example, given: a = array([[9, 2, 3], [4, 5, 6], [7, 0, 5]]) I want to sort the rows of a by the second column to obtain: ...
user avatar
488 votes
11 answers
675k views

How to sort a list of strings?

What is the best way of creating an alphabetically sorted list in Python?
skolima's user avatar
  • 32.1k
485 votes
10 answers
788k views

List<T> OrderBy Alphabetical Order

I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List<T>. 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 ...
SaaS Developer's user avatar
484 votes
4 answers
534k views

How to sort a dataFrame in python pandas by two or more columns?

Suppose I have a dataframe with columns a, b and c, I want to sort the dataframe by column b in ascending order, and by column c in descending order, how do I do this?
Rakesh Adhikesavan's user avatar
479 votes
12 answers
577k views

django order_by query set, ascending and descending

How can I order by descending my query set in django by date? Reserved.objects.all().filter(client=client_id).order_by('check_in') I just want to filter from descending all the Reserved by check_in ...
gadss's user avatar
  • 21.8k
462 votes
12 answers
237k views

How can you sort an array without mutating the original array?

Let's suppose I wanted a sort function that returns a sorted copy of the inputted array. I naively tried this function sort(arr) { return arr.sort(); } and I tested it with this, which shows that ...
Peter Olson's user avatar
462 votes
21 answers
255k views

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 ...
Andrew Coleson's user avatar
459 votes
21 answers
1.3m views

How to sort a List/ArrayList?

I have a List of doubles in java and I want to sort ArrayList in descending order. Input ArrayList is as below: List<Double> testList = new ArrayList(); testList.add(0.5); testList.add(0.2); ...
Himanshu's user avatar
  • 4,771
456 votes
7 answers
842k views

Python list sort in descending order

How can I sort this list in descending order? timestamps = [ "2010-04-20 10:07:30", "2010-04-20 10:07:38", "2010-04-20 10:07:52", "2010-04-20 10:08:...
Rajeev's user avatar
  • 45.3k
445 votes
10 answers
287k views

What is stability in sorting algorithms and why is it important?

I'm very curious, why stability is or is not important in sorting algorithms?
DarthVader's user avatar
  • 53.4k
418 votes
25 answers
81k views

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'...
415 votes
7 answers
71k views

Preserving order with LINQ

I use LINQ to Objects instructions on an ordered array. Which operations shouldn't I do to be sure the order of the array is not changed?
Matthieu Durut's user avatar
413 votes
24 answers
150k views

Is there a built in function for string natural sort?

I have a list of strings for which I would like to perform a natural alphabetical sort. For instance, the following list is naturally sorted (what I want): ['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', '...
snakile's user avatar
  • 53.3k
408 votes
29 answers
231k views

Why is quicksort better than mergesort?

I was asked this question during an interview. They're both O(nlogn) and yet most people use Quicksort instead of Mergesort. Why is that?
Malik Daud Ahmad Khokhar's user avatar
391 votes
8 answers
206k views

orderBy multiple fields in Angular

How to sort by using multiple fields at same time in angular? fist by group and then by sub-group for Example $scope.divisions = [{'group':1,'sub':1}, {'group':2,'sub':10}, {'group':1,'sub':2},{'group'...
gmeka's user avatar
  • 4,269
389 votes
12 answers
832k views

How does one reorder columns in a data frame?

How would one change this input (with the sequence: time, in, out, files): Time In Out Files 1 2 3 4 2 3 4 5 To this output (with the sequence: time, out, in, files)? ...
Catherine's user avatar
  • 5,337
389 votes
12 answers
316k views

Sorting a vector in descending order

Should I use std::sort(numbers.begin(), numbers.end(), std::greater<int>()); or std::sort(numbers.rbegin(), numbers.rend()); // note: reverse iterators to sort a vector in descending order?...
fredoverflow's user avatar
382 votes
7 answers
225k views

How to sort a NSArray alphabetically?

How can I sort an array filled with [UIFont familyNames] into alphabetical order?
DotSlashSlash's user avatar
375 votes
15 answers
357k views

SQL how to make null values come last when sorting ascending

I have a SQL table with a datetime field. The field in question can be null. I have a query and I want the results sorted ascendingly by the datetime field, however I want rows where the datetime ...
David Božjak's user avatar
373 votes
42 answers
427k views

How to sort an array of objects by multiple fields?

From this original question, how would I apply a sort on multiple fields? Using this slightly adapted structure, how would I sort city (ascending) & then price (descending)? var homes = [ {"...
Mike's user avatar
  • 5,648
369 votes
26 answers
574k views

What's the most efficient way to erase duplicates and sort a vector?

I need to take a C++ vector with potentially a lot of elements, erase duplicates, and sort it. I currently have the below code, but it doesn't work. vec.erase( std::unique(vec.begin(), vec.end(...
Kyle Ryan's user avatar
  • 4,311
362 votes
29 answers
728k views

Java Array Sort descending?

Is there any EASY way to sort an array in descending order like how they have a sort in ascending order in the Arrays class? Or do I have to stop being lazy and do this myself :[
AFK's user avatar
  • 4,363

1
2 3 4 5
1544