Okay, so I've been working on a sort function for my application, and I've gotten stuck.
To explain briefly, this code starts with an array of strings, serials
, and an empty array, displaySerials
:
var serials = ["BHU-009", "BHU-008", "BHU-001", "BHU-010", "BHU-002", "TYU-970", "BHU-011", "TYU-969", "BHU-000"]; var displaySerials = [];
The aim of these functions is to output displaySerials
as an array of objects with two properties: beginSerial
and endSerial
. The way that this is intended to work is that the function loops through the array, and tries to set each compatible string in a range with each other, and then from that range create the object where beginSerial
is the lowest serial number in range and endSerial
is the highest in range.
To clarify, all serials in a contiguous range will have the same prefix. Once that prefix is established then the strings are broken apart from the prefix and compared and sorted numerically.
So based on that, the desired output from the array serials
would be:
displaySerials = [ { beginSerial: "BHU-008", endSerial: "BHU-011" }, { beginSerial: "BHU-000", endSerial: "BHU-002" }, { beginSerial: "TYU-969", endSerial: "TYU-970" } ]
I've got it mostly working on my jsfiddle, the only problem is that the function is pushing one duplicate object into the array, and I'm not sure how it is managing to pass my checks.
Any help would be greatly appreciated.