Questions tagged [sorting]
Sorting is the process of applying some order to a collection of items.
77,166
questions
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 ...
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: '...
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 ...
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>();
...
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(...
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}
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
(
[...
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();
...
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 ...
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 ...
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 ....
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 = {
"...
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 ...
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 ...
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.
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. ...
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' }
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 ...
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
[...
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:
...
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 ...
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 ...
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 ...
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 ...
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:
...
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?
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 ...
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?
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 ...
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 ...
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 ...
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);
...
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:...
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?
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?
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', '...
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?
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'...
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)?
...
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?...
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?
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 ...
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 = [
{"...
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(...
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 :[