File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between sorted and ordered? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between sorted and ordered?" Watch "Difference between sorted and ordered?" New topic
Author

Difference between sorted and ordered?

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Jul 03, 2001
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.


SCJP2. Please Indent your code using UBB Code
jioy uilo
Greenhorn

Joined: Mar 29, 2003
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

Joined: Mar 05, 2003
Posts: 1391
A list is an ordered collection.
Is a sorted set an ordered collection?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
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

Joined: Mar 05, 2003
Posts: 1391
If I sort a list, is the result both an ordered collection and a sorted collection?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Jul 03, 2001
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

Joined: Mar 05, 2003
Posts: 1391
Thank you Jose.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: May 07, 2003
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.


Meng Yi
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: May 07, 2003
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between sorted and ordered?