LeonidasFett
LeonidasFett

Reputation: 3132

Sort object array by multiple properties using JQuery

how would I sort the following array so that it first applies a sorting by ascending year span and within each year span, it sorts it first by type "simple" then "complex"? I also need the start index and count of each sorting in the array. So in this case. The problem is that the array can be dynamic, meaning that new year spans can be added.

var array = [
    {"name":"one", "year":"2017-2018", "type":"simple"},
    {"name":"two", "year":"2018-2019", "type":"complex"},
    {"name":"three", "year":"2017-2018", "type":"complex"},
    {"name":"four", "year":"2018-2019", "type":"complex"},
    {"name":"five", "year":"2018-2019", "type":"simple"},
    {"name":"six", "year":"2017-2018", "type":"simple"},
];

The output here would be:

var array = [
    {"name":"one", "year":"2017-2018", "type":"simple"},
    {"name":"six", "year":"2017-2018", "type":"simple"},
    {"name":"three", "year":"2017-2018", "type":"complex"},
    {"name":"five", "year":"2018-2019", "type":"simple"},
    {"name":"two", "year":"2018-2019", "type":"complex"},
    {"name":"four", "year":"2018-2019", "type":"complex"},
];

//These need to be generated dynamically
startIndex2017simple = 0;
count2017simple = 2;
startIndex2017complex = 2;
count2017complex = 1;
startIndex2018simple = 3;
count2018simple = 1;
startIndex2018complex = 4;
count2018complex = 2;

I so far only managed to sort by simple or complex, but not year span.

Here is my current code (TypeScript and React):

props.items.forEach((item, index) => {
    if(item.type=== "simple")
        sorted.unshift(item);
    else
        sorted.push(item);
});

this.simples = props.items.filter((item)=>{
    return item.type === "simple"
});
this.complex= props.items.filter((item)=>{
    return item.type !== "complex"
});      

I get startIndex and count properties by using simples and complex arrays.

Upvotes: 0

Views: 159

Answers (1)

Roshni Kutty
Roshni Kutty

Reputation: 39

@LeonidasFett, I have a javascript logic here to first sort by year. You can improvise this into your React before you sort by type.

var testArray = [
    {"name":"one", "year":"2017-2018", "type":"simple"},
    {"name":"two", "year":"2018-2019", "type":"complex"},
    {"name":"three", "year":"2017-2018", "type":"complex"},
    {"name":"four", "year":"2018-2019", "type":"complex"},
    {"name":"five", "year":"2018-2019", "type":"simple"},
    {"name":"six", "year":"2017-2018", "type":"simple"}
];

testArray.sort(function(a, b) {
    return a.year.substring(0, 4) - b.year.substring(0, 4);
});

logging the testArray to console will now be:

[ 
 {name: "one", year: "2017-2018", type: "simple"}, 
 {name: "three", year: "2017-2018", type: "complex"}, 
 {name: "six", year: "2017-2018", type: "simple"},
 {name: "two", year: "2018-2019", type: "complex"},
 {name: "four", year: "2018-2019", type: "complex"},
 {name: "five", year: "2018-2019", type: "simple"}
]

Then you sort by type.

Upvotes: 1

Related Questions