0

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.

2
  • Could you please show your current sort logic. You just need to amend it so that you sort by year in cases where the type matches. Commented Sep 15, 2017 at 9:34
  • I didn't add it because it was too static and I feel it's a completely wrong approach. Sorry Commented Sep 15, 2017 at 9:38

1 Answer 1

1

@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.

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.