views:

30

answers:

2

Let's say if we are sorting some records by a number in record:

Name  Number_of_Language_Known
John  3
Mary  2
Peter 3
Mike  1
...

If we are not also sorting by Name, then the order of John and Peter is not guaranteed, but it shouldn't be random if the records never changed? I think it should be true in most environment (that is, nothing else changed, and the sorting is done twice).

That is, if we sort it one time, it won't be Peter before John and the second time, John before Peter.

This is because in a Ruby on Rails environment, if the records are fetched from DB and then sorted by a Ruby function, and printed as the original page content, the order is one way, but if the data is requested through AJAX afterwards, then the sorted array elements can have a different order, for records with the same number in that number field, and that seems strange.

Update: if the data is from the db, then maybe the db can have unpredictable order when records are fetched. But what if the records are sorted by primary ID in the first place? Also if the data is right inside the data structure already, I don't know any common sorting algorithm that will produce different sorting order each time. That is, order not guaranteed but not random.

+4  A: 

"not guaranteed" means it can change whenever it likes (depending on the current memory layout, the garbage collector, the time, ...) if you need the sort order of another attribute to be stable you have to sort it by both attributes (by id and then by name - in your case)

Nikolaus Gradwohl
in this case, the data is from the db... but what if the data is not from the db? Most sorting algorithm I know won't product random results. It is not guaranteed in an order, but the order won't be different every time.
動靜能量
also, what if Name can even be the same sometimes? Then I guess you need to sort by a 3rd criterion by Primary ID
動靜能量
Suppose for instance that you have a generic structure called a "List" holding your data. Suppose that this list is implemented under the covers with one data structure up to a certain record count, and then another structure above that count. An example of this is .NET's HybridDictionary. Each of these structures could handle undefined sorts differently and produce different results (I'm not saying HybridDictionary does this--that's just an example)
Michael Haren
@Jian, in the case of possible duplicates, it's not uncommon to sort by many, many fields to remove ambiguity. In my case, I usually sort by the field or two that matter, then by the primary key or log date to make it well defined.
Michael Haren
@Jian `Most sorting algorithm I know won't product random results` not random, but the sorted results can vary based on the initial (dis)order of the unsorted collection. For a list `L`, `s1=sort(L)` and `s2=sort(sort(L))` can definitely produce different orderings when there are ties.
Stephen P
+3  A: 

Assuming these values are coming from an Active Record model, the ordering is coming from the database itself. Different databases handled ordering in different ways, so you will probably need to check the docs for your specific db.

Toby Hede
it is on MySQL 5
動靜能量
A case where this can bite you from the DB is when indexing or volume of data changes. When no sort is requested, the db will return it in whatever method is fastest. When indexes or volumes change, "what's fastest" may change, too, to a new index. The new index could have the data in a different order and thus you get different results.
Michael Haren
I think the answer is: sorting after the data is fetched from the DB won't create random orders. The randomness comes from the DB returning records in possibly random orders.
動靜能量