# Difference between sorted and ordered?

Marlene Miller
Ranch Hand
Posts: 1392
I think I have figured out what the difference is between �sorted� and �ordered�.
The words in this sentence are ordered but not sorted.
and are in ordered sentence sorted. The this words

Marlene Miller
Ranch Hand
Posts: 1392
Well, I thought I understood the difference between sorted and ordered, but now I am not so sure.
in The are but not this also words sorted. ordered sentence

Are the words in the above sentence sorted?
The words are ordered by word length and for a given word length, they are ordered by position within another sentence (the other sentence being: The words in this sentence are also ordered but not sorted.).
Is this ordering a total ordering? If so, does that make the words comparable? If so, are the words sorted?
[ May 23, 2003: Message edited by: Marlene Miller ]

Jose Botella
Ranch Hand
Posts: 2120
hi,
The elements in a sorted collection are mutually comparable either through Comparable or Comparator. The place of the elements in the sequence is given by its value against the other elements.
The elements in a ordered collection are read by an Iterator in the sequence of aggregation to the container. The sequence can be reversed, shuffled or altered with accesses by index.

jioy uilo
Greenhorn
Posts: 25
Ordered sometimes - mean the order which the element are being inserted into the List/Set
and it will be output depending on the sequence of the collection

Marlene Miller
Ranch Hand
Posts: 1392
A list is an ordered collection.
Is a sorted set an ordered collection?

Jose Botella
Ranch Hand
Posts: 2120

Is a sorted set an ordered collection?

According to my previous post it is not. The elements are placed based on their value respect the others, not based on an index or sequence of introduction.

Marlene Miller
Ranch Hand
Posts: 1392
If I sort a list, is the result both an ordered collection and a sorted collection?

Jose Botella
Ranch Hand
Posts: 2120
If you sort a List the sequence is destroyed. Thus it is not an ordered collection.
[ May 25, 2003: Message edited by: Jose Botella ]

Marlene Miller
Ranch Hand
Posts: 1392
If I sort a List and then add an element to the end, the collection is neither ordered nor sorted. Have I violated the contract of a List?

Jose Botella
Ranch Hand
Posts: 2120
Of course not.
_________________________________________________
Let me restate the following:

If you sort a List the sequence is destroyed. Thus it is not an ordered collection.

The previous sequence was destroyed but a new one was born. It just happens that the new one follows a sorting criteria, but still it is possible to access the collection by index; from the beginning to the end in sequence. Thus is an ordered collection.

Marlene Miller
Ranch Hand
Posts: 1392
Thank you Jose.

Anupam Sinha
Ranch Hand
Posts: 1090
Hi Jose
I thought that the order of insertion or last access is considered to be an order. If a list is sorted and if that changes the element's position then would that still be called an ordered list. Because a list, in any case is accessed by an index so whether I sort it, delete an item or do whatever I want, would it still remain ordered. And also by this definition it means that list will always be ordered and there exists nothing as an unordered list. Am i correct?
[ May 26, 2003: Message edited by: Anupam Sinha ]

Marlene Miller
Ranch Hand
Posts: 1392
1. Notice how a list is defined as a sequential arrangement of items:
Normally, we think of linked lists as implementing a sequential arrangement of a set of items: Starting at a given node, we consider its item to be the first in the sequence. Then we follow its link to another node, which gives us an item that we consider to be the second in the sequence, and so forth. Robert Sedgewick, Algorithms in Java

2. Notice how this definition of list does not refer to the items being inserted. Thus we do not have the issue of whether a list being sorted is still a list. A sorted list is still a sequential arrangement of items.
3. Notice how a list that is not in sorted order is assumed to be unordered:
When discussing hashing algorithms and the case when two keys hash to the same address,
we can choose to keep the lists in sorted order, or we can leave them unordered. Robert Sedgewick, Algorithms in Java

[ May 26, 2003: Message edited by: Marlene Miller ]

Yi Meng
Ranch Hand
Posts: 270
To me, for ordered objects, they are just a sequence of objects, the order of these objects is not necessarily to meet any criteria. The key here is consistency, e.g. when you travese a data structure, if the order objects is always the same(provided the structure content remains unchanged), then the structure is said to be ordered.
However, for sorted objects, they have to be ordered acording to a specific criteria. And obviously a sorted structure is always ordered.

Marlene Miller
Ranch Hand
Posts: 1392
Thank you Yi. What you have said makes a lot of sense to me.
That word "ordered" confuses me. I am going to think of a List as a sequence of elements. Or perhaps, as a collection whose elements stay in a sequence unless the list is modified.
A sorted List is a sequence. A SortedSet is not a sequence.
[ May 26, 2003: Message edited by: Marlene Miller ]

Yi Meng
Ranch Hand
Posts: 270
An ordered list of objects may not be of any specific order.
10 random numbers stored in a list will be an ordered list of numbers. (But it is not a sorted list of numbers.)
[ May 26, 2003: Message edited by: Yi Meng ]