misoproject / dataset

JavaScript library that makes managing the data behind client-side visualisations easy
http://misoproject.com
GNU General Public License v2.0
1.18k stars 99 forks source link

Reverse a sort without resorting #135

Closed yanofsky closed 12 years ago

yanofsky commented 12 years ago

There currently doesn't appear to be a way to reverse the sort of a Dataview without resorting. This would be a good method to have.

alexgraul commented 12 years ago

Agreed, however is you reverse and then later re-run sort what do you get? It also becomes a configuration parameter on your comparator. Will have a think.

iros commented 12 years ago

@yanofsky could you provide us with a use case for this scenario? I am struggling to understand when you might need this functionality.

yanofsky commented 12 years ago

Two cases I have in mind.

  1. I have a dataview in displayed in a table and I want to sort on one of the columns when the header is clicked and then sort the oposite direction once it's clicked again.
  2. I have a dataview where some fields are relevantly sorted from large to small and others are from small to large. I only want to write one comparator, and then reverse when sorting on the relevant fields.

To address the @alexgraul question from before perhaps a the reverse method takes an optional boolean that indicates whether the reverse is sticky or not, and the dataview gets a boolean property such as "isReversed" indicating if the view is reversed or not. Calling dataview.reverse(true) would reverse the dataset on subsequent sort() calls. dataview.reverse(false) or .reverse() would only keep the dataview reversed only until the next sort() call.

alexgraul commented 12 years ago

Are you asking for effectively a reversed .each in the first case? Sounds like you'd like to iterate over sorted data backwards. That might make a very sensible addition. For use case 2, can you provide reduced jsFiddle?

yanofsky commented 12 years ago

A reversed .each would work in both cases. Here's a trivial example of case 2 http://jsfiddle.net/2b6Jw/

alexgraul commented 12 years ago

Excellent, I think we have a solution!