1

I'm trying to sort observable arrays.

This fiddle: http://jsfiddle.net/EZUEF/485/ works nicely with a single observablearray. (Click list items to sort)

But this fiddle: http://jsfiddle.net/EZUEF/488/ doesn't work. It's an observablearray containing two objects, each of which contain an observablearray that needs sorting.

I'm aware that this line is wrong: self.FiveFave.FItemGroup.sort(descSort); I think I might need to use event.currenttarget but not sure how.

Thanks in advance.

0

1 Answer 1

1

well you need to do something like this you should pass current reference for sorting .

View:

 <ul data-bind="foreach: $parent.headers">
        <li data-bind="click: function(data,event){ $root.sorter($parent,data,event)}, text: title"></li>
    </ul>

View Model:

 self.sorter = function(data,header, event){
        self.activeSort = header;
        var prop = self.activeSort.sortPropertyName;
        var descSort = function(a,b){ 
            return a[prop] > b[prop] ? -1 : a[prop] < b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0; };
        data.FItemGroup.sort(descSort);
    };

Working Fiddle here

Most important thing here is you should declare

self.FItemGroup = ko.observableArray(fItemGroup); not as self.FItemGroup = fItemGroup;

if you dont do something like that means you are simply inviting a dependency . Guess what happens next if you try to sort first loop data second loop data will also be sorted . Caution advised .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.