Questions tagged [sorted]

This tag refers to a collection of items that has already been arranged in some specific order.

Use this tag for questions dealing with already sorted collections (i.e. reordering them, adding elements to them while still preserving the order, etc.).

297 questions
240
votes
13 answers

How to get indices of a sorted array in Python

I have a numerical list: myList = [1, 2, 3, 100, 5] Now if I sort this list to obtain [1, 2, 3, 5, 100]. What I want is the indices of the elements from the original list in the sorted order i.e. [0, 1, 2, 4, 3] --- ala MATLAB's sort function…
Gyan
  • 2,417
  • 2
  • 12
  • 3
110
votes
5 answers

Is python's sorted() function guaranteed to be stable?

The documentation doesn't guarantee that. Is there any other place that it is documented? I'm guessing it might be stable since the sort method on lists is guaranteed to be stable (Notes 9th point: "Starting with Python 2.3, the sort() method is…
sundar - Remember Monica
  • 6,647
  • 5
  • 40
  • 66
86
votes
12 answers

Sorted array list in Java

I'm baffled that I can't find a quick answer to this. I'm essentially looking for a datastructure in Java which implements the java.util.List interface, but which stores its members in a sorted order. I know that you can use a normal ArrayList and…
Chris Knight
  • 23,000
  • 24
  • 84
  • 132
85
votes
7 answers

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 list functions or methods other than [], [:], +,…
Will S
  • 901
  • 1
  • 7
  • 9
45
votes
4 answers

python sort list of json by value

I have a file consists of JSON, each a line, and want to sort the file by update_time reversed. sample JSON file: { "page": { "url": "url1", "update_time": "1415387875"}, "other_key": {} } { "page": { "url": "url2", "update_time": "1415381963"},…
icycandy
  • 945
  • 2
  • 10
  • 18
36
votes
3 answers

JPA/hibernate sorted collection @OrderBy vs @Sort

I would like to have a collection of child objects (here cat-kitten example) that are ordered. And keep their order on adding of new elements. @Entity public class Cat { @OneToMany(mappedBy = "cat", cascade = CascadeType.ALL) @OrderBy("name…
adler
  • 909
  • 1
  • 9
  • 10
26
votes
8 answers

Automatically sorted by values map in Java

I need to have an automatically sorted-by-values map in Java - so that It keeps being sorted at any time while I'm adding new key-value pairs or update the value of an existing key-value pair, or even delete some entry. Please also have in mind…
Alexandros
  • 3,529
  • 3
  • 18
  • 19
18
votes
6 answers

Will a Python dict with integers as keys be naturally sorted?

If I create a Python dict which uses integers as keys, can I safely assume that iterating over the dict will retrieve items in order according to key value? i.e. will my_dict = {} for x in range(0,100): my_dict[x] = str(x) for item in…
user3067927
  • 223
  • 1
  • 2
  • 5
16
votes
1 answer

What is wrong with my code - circularly sorted array does not show any results

I had an interview today and the person asked me this question: How do you find easily an item in a circularly sorted array Since I didn't know the answer, I tried to find a solution. Here's what I have: Thanks
Gino Sullivan
  • 2,193
  • 16
  • 28
13
votes
1 answer

What are better ways to insert element in sorted array in PHP

I've recently send my CV to one company that was hiring PHP developers. They send me back a task to solve, to mesure if I'm experienced enough. The task goes like that: You have an array with 10k unique elements, sorted descendant. Write function…
piotrekkr
  • 2,507
  • 2
  • 17
  • 28
12
votes
3 answers

C# Iterate over Dictionary sorted by value

Is there any way to iterate over a Dictionary, in sorted order, sorted by VALUE not key? I did read abut the "SortedDictionary" object, but sadly, that is sorted by key. One solution would be for me to flip all of my keys with my values, and place…
Georges Oates Larsen
  • 5,924
  • 10
  • 43
  • 63
12
votes
3 answers

Laravel saving ordered list of eloquent models

I'm creating a food menu which the administrator can order/sort by dragging and dropping. This menu consists of multiple categories (ProductCategory) and products (Product). I'm using HTML5Sortable on the client-side to allow nested d&d. The markup…
JasonK
  • 4,654
  • 9
  • 29
  • 58
12
votes
1 answer

JavaFX: TableView: Arrow for a column sorted by default

I have a simple java fx application which has a table view. The table view shows some data that is sorted in a certain order. Is there a way to show the arrow(by default) indicating that the data has been sorted in that order? My code is…
Aspirant
  • 1,884
  • 2
  • 23
  • 43
10
votes
2 answers

Java stream operation fusion and stateful intermediate operations

I have been trying to understand and showcase how Java streams implement a type of loop fusion under the hood, so that several operations can be fused into a single pass. This first example here: Stream.of("The", "cat", "sat", "on", "the", "mat") …
Tranquility
  • 2,511
  • 3
  • 17
  • 32
10
votes
5 answers

Algorithm to merge multiple sorted sequences into one sorted sequence in C++

I am looking for an algorithm to merge multiple sorted sequences, lets say X sorted sequences with n elements, into one sorted sequence in c++ , can you provide some examples? note: I do not want to use any library
user2970210
  • 355
  • 1
  • 3
  • 8
1
2 3
19 20