[英]How to sort objects in an array by date without using the sort method?
對於我必須為學校做的項目,我必須制作一個可以按日期和時間對約會進行排序的應用程序。 我有一個包含對象的數組,但由於日期是嵌套的,因此無法弄清楚如何按日期對其進行排序。
這是我制作的冒泡排序功能:
function bubbleSort() {
const loop = listOfAppointments.length;
for(let i = 0; i < loop; i++) {
for(let j = 0; j < loop; j++) {
if(listOfAppointments[j] > listOfAppointments[j+1]) {
let temp = listOfAppointments[j];
listOfAppointments[j] = listOfAppointments[j+1];
listOfAppointments[j+1] = temp;
}
}
}
}
這個函數可以很好地處理數字,但我不知道如何使用這個函數對對象進行排序。 我知道javascript中有一個排序功能,但我們不允許使用它。 我嘗試排序的數組如下所示:
[
{
"Appointment": {
"Id": 2,
"nameCustomer": "Henk Jan",
"addresdCustomer": "somethingstreet 34, middleofnowhere",
"time": "2020-01-07T10:00:00Z",
"reason": "gibberish"
}
},
{
"Appointment": {
"Id": 1,
"nameCustomer": "Jan Jaap",
"addresdCustomer": "somethingpavilion 54, middleofnowhere",
"time": "2020-01-07T12:15:00Z",
"reason": "gibberish"
}
},
{
"Appointment": {
"Id": 3,
"nameCustomer": "So Lost",
"addresdCustomer": "somethingthere 234, middleofnowhere",
"time": "2020-01-07T11:30:00Z",
"reason": "gibberish"
}
},
...
]
謝謝!
嘗試這個
function bubbleSort() {
const loop = listOfAppointments.length;
for(let i = 0; i < loop; i++) {
for(let j = i+1; j < loop; j++) {
if(new Date(listOfAppointments[i].Appointment.time) > new Date(listOfAppointments[j].Appointment.time)) {
let temp = listOfAppointments[i];
listOfAppointments[i] = listOfAppointments[j];
listOfAppointments[j] = temp;
}
}
}
}
從我記得的冒泡排序算法。
它應該是這樣的。
時間被轉換為日期進行比較。
第二個循環將最高的推到最后。
因此,每次運行時,它都需要少循環 1 個索引。
function bubbleSortAppointments(arr) { for(let i = arr.length - 1; i > 0; i--) { for(let j = 0; j < i; j++) { let date1 = new Date(arr[j].Appointment.time); let date2 = new Date(arr[j+1].Appointment.time); if(date1.getTime() > date2.getTime()) { let temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } let listOfAppointments = [ { "Appointment": { "Id": 2, "nameCustomer": "Henk Jan", "addressCustomer": "somethingstreet 34, middleofnowhere", "time": "2020-01-07T10:00:00Z", "reason": "gibberish" } }, { "Appointment": { "Id": 1, "nameCustomer": "Jan Jaap", "addressCustomer": "somethingpavilion 54, middleofnowhere", "time": "2020-01-07T12:15:00Z", "reason": "gibberish" } }, { "Appointment": { "Id": 3, "nameCustomer": "So Lost", "addressCustomer": "somethingthere 234, middleofnowhere", "time": "2020-01-07T11:30:00Z", "reason": "gibberish" } } ]; bubbleSortAppointments(listOfAppointments); console.log(listOfAppointments);
首先,您不需要外部循環(計算i
變量的循環),因為它根本沒有使用。
我建議更改您的bubbleSort
函數以接受兩個參數:要排序的列表和謂詞函數。 謂詞函數應該接受兩個參數(每個參數都是一個約會)並且它應該返回true
/ false
。 使用謂詞的結果進行排序:如果謂詞返回true
則進行排序,否則不進行排序。
下面是一個工作實現。 earliest
和mostRecent
函數是謂詞。
注意:顯示的bubbleSort
函數是 pure ,這意味着它不會修改給定的列表參數,而是創建列表的副本並對副本進行排序。 你不必做這樣,如果你不想。 但是,我會鼓勵你這樣做。
const appointments = [ { "Appointment": { "Id": 2, "nameCustomer": "Henk Jan", "addresdCustomer": "somethingstreet 34, middleofnowhere", "time": "2020-01-07T10:00:00Z", "reason": "gibberish" } }, { "Appointment": { "Id": 1, "nameCustomer": "Jan Jaap", "addresdCustomer": "somethingpavilion 54, middleofnowhere", "time": "2020-01-07T12:15:00Z", "reason": "gibberish" } }, { "Appointment": { "Id": 3, "nameCustomer": "So Lost", "addresdCustomer": "somethingthere 234, middleofnowhere", "time": "2020-01-07T11:30:00Z", "reason": "gibberish" } } ]; // bubbleSort :: Array -> Function -> Array function bubbleSort(list, predicate) { const size = list.length - 1; // <-- or last item will produce errors! const clone = list.slice(); // <-- clone of given list, just to be pure for(let j = 0; j < size; j++) { if(predicate(clone[j], clone[j+1])) { // <-- this line let temp = clone[j]; clone[j] = clone[j+1]; clone[j+1] = temp; } } return clone; } // earliest :: Appointment -> Appointment -> Boolean function earliest (a, b) { return Date.parse(a.Appointment.time) > Date.parse(b.Appointment.time); } // mostRecent :: Appointment -> Appointment -> Boolean function mostRecent (a, b) { return Date.parse(a.Appointment.time) < Date.parse(b.Appointment.time); } console.log('Earliest', bubbleSort(appointments, earliest)) console.log('Most recent', bubbleSort(appointments, mostRecent))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.