0
我將如何對以下數組進行排序,以便它首先按升序年份跨度和每年跨度應用排序,它首先按類型「簡單」,然後「複雜」對它進行排序?我還需要數組中每個排序的開始索引和計數。所以在這種情況下。問題是數組可以是動態的,這意味着可以添加新的一年跨度。使用JQuery將對象數組按多個屬性排序
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"},
];
這裏的輸出是:
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;
我至今只設法通過簡單或複雜的,但不是年的跨度進行排序。
這裏是我當前的代碼(打字稿和反應):
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"
});
我得到startIndex和利用simples和複雜的陣列計算性能。
您能否請您顯示您當前的排序邏輯。你只需要修改它,以便在'type'匹配的情況下按'year'排序。 –
我沒有添加它,因爲它太靜態,我覺得這是一個完全錯誤的方法。抱歉 – LeonidasFett