chustar
chustar

Reputation: 12455

Efficient way of unsorting an array I sorted myself

In my application, I let users choose how they want their data arranged (sorted or unsorted).
When they choose sorted, I simply sort everything. That part is straightforward to me.

However, when they choose sorted, and then choose unsorted, I want to show them the data in the same order that the database gave it to me (i.e. I don't want to just call randomize() or something).
Is there an efficient way of "unsorting" an array that I sorted myself without maintaing a copy of the original array?

Upvotes: 1

Views: 82

Answers (2)

Erik Kaplun
Erik Kaplun

Reputation: 38217

Why not just keep a copy of the original collection? Or just never modify it in place and instead generate sorted copies of the original collection as the user requests sorting. This seems like much simpler than keeping an original-order value or something like that.

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 992857

One thing you could do is keep an extra (hidden) integer value in each record which records the original order that you received the records from the database. When the user chooses "unsorted", then sort by that original-order value.

Upvotes: 6

Related Questions