All Questions
4,704
questions
2780
votes
22
answers
1.4m
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}, ...
1636
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>();
...
1231
votes
10
answers
964k
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 ....
911
votes
11
answers
951k
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. ...
638
votes
20
answers
620k
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 ...
485
votes
10
answers
796k
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 ...
464
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);
...
464
votes
7
answers
859k
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:...
349
votes
11
answers
653k
views
How to convert a Collection to List?
I am using TreeBidiMap from the Apache Collections library. I want to sort this on the values which are doubles.
My method is to retrieve a Collection of the values using:
Collection coll = themap....
295
votes
7
answers
154k
views
What is the difference between `sorted(list)` vs `list.sort()`?
list.sort() sorts the list and replaces the original list, whereas sorted(list) returns a sorted copy of the list, without changing the original list.
When is one preferred over the other?
Which is ...
285
votes
29
answers
799k
views
Finding median of list in Python
How do you find the median of a list in Python? The list can be of any size and the numbers are not guaranteed to be in any particular order.
If the list contains an even number of elements, the ...
272
votes
10
answers
354k
views
From list of integers, get number closest to a given value
Given a list of integers, I want to find which number is the closest to a number I give in input:
>>> myList = [4, 1, 88, 44, 3]
>>> myNumber = 5
>>> takeClosest(myList, ...
242
votes
16
answers
236k
views
Given parallel lists, how can I sort one while permuting (rearranging) the other in the same way?
Suppose I have:
list1 = [3, 2, 4, 1, 1]
list2 = ['three', 'two', 'four', 'one', 'one2']
Calling list1.sort() will sort it, resulting in [1, 1, 2, 3, 4]. However, can I get list2 to be rearranged in ...
234
votes
31
answers
346k
views
How can I reverse a Java 8 stream and generate a decrementing IntStream of values?
General question: What's the proper way to reverse a stream? Assuming that we don't know what type of elements that stream consists of, what's the generic way to reverse any stream?
Specific question:...
228
votes
13
answers
234k
views
Sort a list of objects in Flutter (Dart) by property value
How to sort a list of objects by the alphabetical order of one of its properties (Not the name but the actual value the property holds)?
225
votes
16
answers
562k
views
How can I sort a List alphabetically?
I have a List<String> object that contains country names. How can I sort this list alphabetically?
225
votes
6
answers
486k
views
Python data structure sort list alphabetically [duplicate]
I am a bit confused regarding data structure in python; (),[], and {}. I am trying to sort a simple list, probably since I cannot identify the type of data I am failing to sort it.
My list is simple: ...
221
votes
9
answers
111k
views
case-insensitive list sorting, without lowercasing the result?
I have a list of strings like this:
['Aden', 'abel']
I want to sort the items, case-insensitive.
So I want to get:
['abel', 'Aden']
But I get the opposite with sorted() or list.sort(), because ...
201
votes
27
answers
186k
views
Pythonic way to check if a list is sorted or not
Is there a pythonic way to check if a list is already sorted in ASC or DESC
listtimestamps = [1, 2, 3, 5, 6, 7]
something like isttimestamps.isSorted() that returns True or False.
I want to input a ...
181
votes
7
answers
451k
views
C# list.Orderby descending
I would like to receive a List sorted by Product.Name in descending order.
Similar to the function below which sorts the list in ascending order, just in reverse, is this possible?
var newList = list....
171
votes
10
answers
10k
views
Is there a way to measure how sorted a list is?
Is there is a way to measure how sorted a list is?
I mean, it's not about knowing if a list is sorted or not (boolean), but something like a ratio of "sortness", something like the coefficient of ...
170
votes
7
answers
257k
views
Sort a list of lists with a custom compare function
I know there are several questions named like this, but they don't seem to work for me.
I have a list of lists, 50 times 5 elements. I want to sort this list by applying a custom compare function to ...
170
votes
9
answers
129k
views
Does python have a sorted list?
By which I mean a structure with:
O(log n) complexity for x.push() operations
O(log n) complexity to find an element
O(n) complexity to compute list(x) which will be sorted
I also had a related ...
169
votes
10
answers
471k
views
Sorting a list with stream.sorted() in Java
I'm interested in sorting a list from a stream. This is the code I'm using:
list.stream()
.sorted((o1, o2)->o1.getItem().getValue().compareTo(o2.getItem().getValue()))
.collect(Collectors....
163
votes
8
answers
169k
views
Sorting Python list based on the length of the string
I want to sort a list of strings based on the string length. I tried to use sort as follows, but it doesn't seem to give me correct result.
xs = ['dddd','a','bb','ccc']
print xs
xs.sort(lambda x,y: ...
142
votes
2
answers
189k
views
Python - How to sort a list of lists by the fourth element in each list? [duplicate]
I would like to sort the following list of lists by the fourth element (the integer) in each individual list.
unsorted_list = [['a','b','c','5','d'],['e','f','g','3','h'],['i','j','k','4','m']]
How ...
132
votes
10
answers
206k
views
Insert an item into sorted list in Python
I'm creating a class where one of the methods inserts a new item into the sorted list. The item is inserted in the corrected (sorted) position in the sorted list. I'm not allowed to use any built-in ...
130
votes
8
answers
124k
views
Shuffle two list at once with same order [duplicate]
I'm using the nltk library's movie_reviews corpus which contains a large number of documents. My task is get predictive performance of these reviews with pre-processing of the data and without pre-...
127
votes
6
answers
196k
views
Custom Python list sorting
I was refactoring some old code of mine and came across of this:
alist.sort(cmp_items)
def cmp_items(a, b):
if a.foo > b.foo:
return 1
elif a.foo == b.foo:
return 0
...
119
votes
2
answers
265k
views
How do I sort a list of datetime or date objects?
How do I sort a list of date and/or datetime objects? The accepted answer here isn't working for me:
from datetime import datetime,date,timedelta
a=[date.today(), date.today() + timedelta(days=1), ...
99
votes
23
answers
121k
views
In Java how do you sort one list based on another?
I've seen several other questions similiar to this one but I haven't really been able to find anything that resolves my problem.
My use case is this: user has a list of items initially (listA). They ...
98
votes
13
answers
75k
views
Is it faster to sort a list after inserting items or adding them to a sorted list
If I have a sorted list (say quicksort to sort), if I have a lot of values to add, is it better to suspend sorting, and add them to the end, then sort, or use binary chop to place the items correctly ...
97
votes
15
answers
129k
views
Sorting an IList in C#
So I came across an interesting problem today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it.
Turns out the IList interface doesn't have a sort ...
92
votes
7
answers
100k
views
How can I sort a list of strings in Dart?
I see in the API docs there is a sort() method on List, but I'm not clear what it needs for a parameter. The current need is for a very simple straight up alpha comparison.
87
votes
22
answers
135k
views
Combining two sorted lists in Python
I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to ...
84
votes
3
answers
45k
views
Sort list while pushing None values to the end
I have a homogeneous list of objects with None, but it can contain any type of values.
Example:
>>> l = [1, 3, 2, 5, 4, None, 7]
>>> sorted(l)
[None, 1, 2, 3, 4, 5, 7]
>>> ...
84
votes
6
answers
171k
views
How to sort and remove duplicates from Python list? [duplicate]
Given a list of strings, I want to sort it alphabetically and remove duplicates. I know I can do this:
from sets import Set
[...]
myHash = Set(myList)
but I don't know how to retrieve the list ...
78
votes
3
answers
131k
views
Java 8: sort list of objects by attribute without custom comparator
What is the cleanest short way to get this done ?
class AnObject{
Long attr;
}
List<AnObject> list;
I know it can be done with custom comparator for AnObject. Isn't there something ready ...
76
votes
2
answers
90k
views
django - convert a list back to a queryset [duplicate]
I have a handful of records that I would like to sort based on a computed value. Got the answer over here... like so:
sorted(Profile.objects.all(), key=lambda p: p.reputation)
on a Profile class ...
75
votes
6
answers
40k
views
Sort one list by another
I have 2 list objects, one is just a list of ints, the other is a list of objects but the objects has an ID property.
What i want to do is sort the list of objects by its ID in the same sort order as ...
74
votes
2
answers
36k
views
Git: List git branches, sort by (and show) date [duplicate]
How can I list git branches showing and sorting by their last commits' dates?
I've found this:
for k in `git branch | sed s/^..//`; do
echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%...
69
votes
4
answers
162k
views
Sort a List of Object in VB.NET
I have a list of passengers(object) where it has a differents properties..
passenger.name
passenger.age
passenger.surname
And I want to sort this list by age criterion, how can i do this?
I know in ...
66
votes
3
answers
32k
views
Python: How to sort a list of dictionaries by several values?
I want to sort a list at first by a value and then by a second value. Is there an easy way to do this? Here is a small example:
A = [{'name':'john','age':45},
{'name':'andi','age':23},
{'...
65
votes
17
answers
127k
views
Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?
In Java you can build up an ArrayList with items and then call:
Collections.sort(list, comparator);
Is there anyway to pass in the Comparator at the time of list, creation like you can do with ...
62
votes
3
answers
20k
views
Difference between Collections.sort(list) and list.sort(Comparator)
Is there any reason why I should prefer Collections.sort(list) method over simply calling the list.sort()? Internally Collections.sort merely calls the sort method of the List class anyway.
It's just ...
60
votes
4
answers
123k
views
Sort multidimensional array based on 2nd element of the subarray
I have an array like this:
[['G', 10], ['A', 22], ['S', 1], ['P', 14], ['V', 13], ['T', 7], ['C', 0], ['I', 219]]
I'd like to sort it based on the 2nd element in descending order.
An ideal output ...
58
votes
3
answers
37k
views
List.shuffle() in Dart?
I'm looking every where on the web (dart website, stackoverflow, forums, etc), and I can't find my answer.
So there is my problem: I need to write a function, that print a random sort of a list, ...
55
votes
9
answers
115k
views
Sort List in reverse in order
I have List list1 in direct order. List<String> list = Ordering.natural().sortedCopy(asu2);
How to change order. And I don't know how to rewrite methods from extends class, please write with ...
55
votes
3
answers
47k
views
How can I sort a List<T> by multiple T.attributes?
Let's say I have a List of Songs.
Song {
public string Name = "";
public int PlayOrder = 0;
}
Now I want to sort them first by PlayOrder starting at zero and second by Name ...
55
votes
6
answers
178k
views
How to sort multidimensional array by column?
Is there a way to use the sort() method or any other method to sort a list by column? Lets say I have the list:
[
[John,2],
[Jim,9],
[Jason,1]
]
And I wanted to sort it so that it would look like ...