# Sort the array of objects with a property with a number separated by points

I have an array of objects like this

``````var arr = [{
"id": "1",
"Title": "Object1",
"SectionId": "1.2.1"
}, {
"id": "2",
"Title": "Object2",
"SectionId": "1.1.2"
}, {
"id": "3",
"Title": "Object3",
"SectionId": "1.0.1"
}, {
"id": "4",
"Title": "Object4",
"SectionId": "1"
}];
```
```

`sectionId` has number separated dots. How do I sort the array based on the `sectionId` property in ascending or descending order?

Inspired by Sorting with map

``````function customSort(d, order) {
var sort = {
asc: function (a, b) {
var l = 0, m = Math.min(a.value.length, b.value.length);
while (l < m && a.value[l] === b.value[l]) {
l++;
}
return l === m ? a.value.length - b.value.length : a.value[l] - b.value[l];
},
desc: function (a, b) {
return sort.asc(b, a);
}
},

// temporary array holds objects with position and sort-value
mapped = d.map(function (el, i) {
return { index: i, value: el.SectionId.split('.').map(Number) };
});

// sorting the mapped array containing the reduced values
mapped.sort(sort[order] || sort.asc);

// container for the resulting order
return mapped.map(function (el) {
return d[el.index];
});
}

var arr = [{ "id": "1", "Title": "Object1", "SectionId": "1.2.1" }, { "id": "2", "Title": "Object2", "SectionId": "1.1.2" }, { "id": "3", "Title": "Object3", "SectionId": "1.0.1" }, { "id": "4", "Title": "Object4", "SectionId": "1.1.10" }, { "id": "5", "Title": "Object5", "SectionId": "1" }];

document.write('<pre>sorted array asc ' + JSON.stringify(customSort(arr), 0, 4) + '</pre>');
document.write('<pre>sorted array desc ' + JSON.stringify(customSort(arr, 'desc'), 0, 4) + '</pre>');
document.write('<pre>original array ' + JSON.stringify(arr, 0, 4) + '</pre>');```
```