Otrzymuję obiekt json z Facebooka, który zawiera informacje o znajomych.
Niektórzy użytkownicy podali swoje urodziny, inni nie, a inni tylko miesiąc i dzień.
Chcę posortować tablicę, umieszczając w pierwszej kolejności użytkowników, których data urodzenia jest zbliżona do daty bieżącej.
Jak mogę to zrobić?
Obiekt json wygląda następująco:
json = { "data" : [{name : "Joe Sam", id : "5555555", birthday: "02/02/1989" }, {name : "Joe Sam", id : 5555555, birthday: }, {name : "Joe Sam", id : 5555555, birthday: "01/01" }
Odpowiedzi:
1 dla odpowiedzi № 1Twój JSON jest nieprawidłowy - jeśli to jest rzeczywisty JSONnazwy kluczy w łańcuchach muszą być cytowane. Pozostawiłeś zamykające] i}, a data urodzenia środkowego rekordu musi mieć jakąś wartość, np. Pusty ciąg lub null - albo po prostu nie podawaj tego klucza w ogóle. Zakładam, że możesz to naprawić i już przeanalizowałeś JSON w zmiennej o nazwie json
.
Nie mówisz też, czy daty są w formacie DD / MM (/ RRRR) czy MM / DD (/ RRRR), więc będę kodować dla DD / MM, ale możesz to skomentować, aby zamiast tego użyć MM / DD.
„Najbliższa data” jest niejednoznaczna: czy wczorajszy dzień jest bliżej niż w przyszłym tygodniu? Zakładam, że wczoraj jest tak daleko od aktualnej daty, jak to tylko możliwe.
Więc oto twój obiekt uporządkowany wraz z procedurą sortowania. Nie testowałem go, ale nawet zakładając, że jest uszkodzony, powinien dać ci ogólny pomysł:
var json = { "data" : [
{name : "Joe Sam", id : "5555555", birthday: "02/02/1989" },
{name : "Joe Sam", id : 5555555, birthday: null },
{name : "Joe Sam", id : 5555555, birthday: "01/01" }
]
};
// First sort into ascending birthday order, with people who didn"t provide
// a birthday at the beginning of the list
function dayMonthComparer(a,b)
// note double-equals null also allows for undefined "birthday" property
if (aBD == null)
return bBD == null ? 0 : -1;
if (bBD == null)
return 1;
// next two lines allow for DD/MM format; comment them out for MM/DD format
aBD = aBD.substr(3,2) + aBD.substr(0,2);
bBD = bBD.substr(3,2) + bBD.substr(0,2);
// note: simple string compare works once in MM/DD format
return aBD === bBD ? 0 : (aBD > bBD ? 1 : -1);
}
json["data"].sort(function(a,b) {
return dayMonthComparer(a["birthday"],b["birthday"]);
});
// Next, find the first item in the array after the current date and
// move everything before that item to the end of the array.
var today = new Date(),
d = today.getDate(),
m = today.getMonth() + 1,
current,
firstNonBlank = null,
firstFromCurrent = 0;
if (d < 10) d = "0" + d;
if (m < 10) d = "0" + d;
current = d + "/" m;
// or use current = m + "/" + d if using American format
// get index of first item with birthday on or after current date
while(firstFromCurrent < json["data"].length &&
dayMonthComparer(current,json["data"][firstFromCurrent]["birthday"]) > 1) {
if (firstNonBlank===null &&
json["data"][firstFromCurrent]["birthday"] != null)
firstNonBlank = firstFromCurrent;
firstFromCurrent++;
}
if (firstFromCurrent < json["data"].length) {
json["data"] = json["data"].slice(firstFromCurrent)
.concat(json["data"].slice(firstNonBlank,firstFromCurrent),
json["data"].slice(0,firstNonBlank) );
}
// array is now sorted by birthday starting from current date, where
// those who didn"t provide a birthday are at the end
Aby uzyskać szczegółowe informacje o tym, jak to zrobić .sort()
prace odnoszą się do MDN doco.