161

Is there any difference between a sorted and an ordered collection?

Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
Johanna
  • 27,036
  • 42
  • 89
  • 117
  • 2
    Do not take the answers here too literally. While that is the kind of definition widely understood & acknowledged, its not the de facto definition in computer terminology. For eg, in .NET the interface for "sorted" enumerable is called `IOrderedEnumerable` *(funny thing is its not very consistent in .NET. An "insertion order" respectin dictionary in .NET is called `OrderedDictionary` which some believe is a misnomer as compared to say, `IndexedDictionary`)*. Yes in java world (mostly elsewhere too) they mean what u've in answers. For more see [here](http://stackoverflow.com/questions/1022269). – nawfal May 21 '14 at 17:01
  • If some implementation instance gets the naming wrong, that's no reason to propagate its error. It's a good question, with good answers. Use proper naming - helps reducing confusion for everybody, including yourself. – foo Feb 23 '19 at 18:35

8 Answers8

197

An ordered collection means that the elements of the collection have a specific order. The order is independent of the value. A List is an example.

A sorted collection means that not only does the collection have order, but the order depends on the value of the element. A SortedSet is an example.

In contrast, a collection without any order can maintain the elements in any order. A Set is an example.

zaPlayer
  • 787
  • 5
  • 24
g .
  • 8,110
  • 5
  • 38
  • 48
  • 1
    Is Priority queue an ordered collection? – overexchange Oct 12 '17 at 20:02
  • @overexchange Given the above definitions, a priority queue would be a sorted collection in most cases, since the priority is almost always defined as a property of the elements in the queue. – cdeszaq Oct 26 '18 at 18:04
  • If SortedSet has inherited from Set and is having a is-a relationship with it then how can u say Set is without any order – xpioneer May 17 '19 at 07:21
88

An ordered collection maintains the order of the elements based on the sequence you put stuff into/remove them from the collection.

A sorted collection keeps the elements sorted based on a sort criteria.

nos
  • 223,662
  • 58
  • 417
  • 506
24

Java uses "ordered collection" to mean a collection such as List, where (unlike HashSet), the collection remembers what order the elements are supposed to be in. So elements can be added to the collection at a particular "place" in the order.

Java uses "sorted collection" to mean a collection such as SortedSet, where (unlike List), the order that the iterator traverses the collection is in accordance with a specified Comparator or the natural order of the elements.

So the difference is whether the ordering depends on the values ("sorted"), or is a property that elements have independently of their value ("ordered").

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • 2
    Good answer and +1 for mentioning "Java". Its *kinda* the accepted definition in most places too, like `OrderedDict` in python. But in .NET the interface for "sorted" enumerable is called `IOrderedEnumerable`. So it depends. Just saying.. – nawfal May 21 '14 at 17:07
14

Yes, though the concepts are similar.

List is an ordered collection: each element has an index, which forms an ordering of the elements, but not usually related to any property of the elements themselves.

SortedMap and SortedSet are sorted collections, which means that iteration through the collection will happen in a sequence derived from the elements themselves. For example, if you have a SortedSet<String> then the Strings will be sorted according to the lexicographical sort order.

An ordered Collection can be sorted but doesn't have to be (e.g. after using Collections.sort()) when the external ordering is identical with the elements' sort order. A sorted collection is always implicitly ordered (i.e. there is always a "first" element, and it's always the same as long as you don't add another, smaller one).

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
7

An ordered collection is a collection that keep track of a consecutive index which every element is inserted in.

A sorted collection is an ordered collection when the order additionally depends on the value of the element to be inserted in, throughout the use of the Comparable interface which provides you with a method to define the sorting criteria.

I hope it could help.

gvalenncia
  • 261
  • 3
  • 9
2

Sorted would imply ordering according to an implementation of Comparable or Comparator. Ordered would imply that it is following the insertion order or some other definition of order that is consistent and defined, but otherwise arbitrary.

So a sorted list of strings would be sorted according to the String.compareTo method. A list might contain a list of strings inserted in arbitrary order, but that order will always remain the same.

Of course there are methods on the Collections class to sort a list.

Yishai
  • 90,445
  • 31
  • 189
  • 263
2

A sorted collection usually mean the elements are sorted from minimun value to maxinum value or vice versa depending on the attribute(s) of the elements on which algorithms work.

for a interger collections, the sorted may be from min number to max number for a person collection, it may be sored by the height of persons or the weight of persons, etc.

When talking about order, it usually means the order of insertion. The order may be changed after sorting

sotondolphin
  • 223
  • 1
  • 10
0

Sorted Collection vs. Ordered Collection

1. Sorted collection

A sorted collection is sorting a collection by utilizing the sorting features provided by the Java collections framework. The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator.

If your collection is not large, it will be more efficient way to sort it. As it happens in jvm memory, it can throw Out of Memory error.

2. Order collection

Order collection is sorting a collection by specifying the order-by clause in query for sorting this collection when retrieval. If your collection is very large, it will be more efficient way to sort it. It is fast compared to sorted collection.