before my sort, array : "86", "85", "89"
Looks like you have a sparse array: An array of length 90
with items at the indices 85
, 86
and 89
.
after my sort, array : "0", "1", "2"
. Is this normal?
Yes. Non-existent properties (0
-84
, 87
, 88
) are ignored by sort
. From the spec:
NOTE 1: Because non-existent property values always compare greater than undefined
property values, and undefined
always compares greater than any other value, undefined
property values always sort to the end of the result, followed by non-existent property values.
how i can change that ?
You cannot. However, there's no reason to have anything else than that, as you wanted to sort your array, which means reordering the items. Array indices are no ids. If your objects should have ids, make them properties:
var myArray = [
{"id":"86", "target_area": {"name_area": "Loic"}},
{"id":"85", "target_area": {"name_area": "aaaaaaaa"}},
{"id":"81", "target_area": {"name_area": "zzzzzzzzzz"}}
];
Of course, there are workarounds like copying the values to a different array, sorting that and moving them back into the sparse array - see javascript sort sparse array keep indexes. You don't seem to need that, though.