Q:

In Knockout.js, how to sort viewModel item based on checkbox field after mapping

Data example, where VoucherRedeemed is a checkbox value:
{
 VoucherTitle: "title 2",
 VoucherRedeemed: false,
},{
 VoucherTitle: "title 4",
 VoucherRedeemed: true,
},{
 VoucherTitle: "title 6",
 VoucherRedeemed: false,
},{
 VoucherTitle: "title 9",
 VoucherRedeemed: true,
}

How do I sort the viewModel.voucher based on the checkbox value, after mapping:
viewModel.voucher = ko.mapping.fromJS(voucherData.voucher)

I've had a look at sorting observable arrays, but did not manage to tie up to the mapping scenario for checkbox value sorting. (http://knockoutjs.com/documentation/observableArrays.html)
Any example of how to sort an array after ko.mapping.fromJS will be appreciated!

A:

You can sort once you have populated the view model.
Have a look at the fiddle I have put together here Knockout Sort Example
You need to call the sort method on the observable passing in a function to sort on the property you want.

A:

I figured.
Sort the voucherData before mapping it into the viewModel. Once it is in the viewModel, not sure if it is possible or easy to do any sorting.

