I'm doing this assignment were they have told me to sort an array of objects with name and age. What do you think about my solution?
Edited to take in consideration comments, Thanks for the feedback guys!
var familyAgesPropName = [ { name: "Raul", age: 27 }, { name: "Jose", age: 55 }, { name: "Maria", age: 52 }, { name: "Jesus", age: 18 }, { name: "Neo", age: 2 } ]; var familyAgesWithoutPropName = [ { "Raul": 27 }, { "Jose": 55 }, { "Maria": 52 }, { "Jesus": 18 }, { "Neo": 2 } ]; var familyAgesWithoutPropNameMissingAge = [ { "Raul": 27 }, { "Jose": 55 }, { "Maria": '' }, { "Jesus": 18 }, { "Neo": 2 } ]; var familyAgesWithoutPropNameMissingName = [ { "Raul": 27 }, { "Jose": 55 }, { 52: "" }, { "Jesus": 18 }, { "Neo": 2 } ]; var familyAgesWithoutPropNameMissingNameAndNULL = [ null, { "Raul": 27 }, { "Jose": 55 }, { 52: "" }, { "Jesus": 18 }, { "Neo": 2 } ]; /** @brief: cleaningAndFormatting is a function that takes the input array (that I assume can come in any way) and converts it to a proper format that is correct for using and outputing it. The format of my choice is [{name: String, age: Int}, item2, ...] @param: array with the data. @notes: If the input array comes already in the desired format we can comment this function improving the performance of the process If the name is actually a number (only digits) then we put it infront to see that we have a problem with it If the age is empty or a string that doesn't make sense we assign 0 to put it after the problmatics **/ // var cleaningAndFormatting = (function(array) { // for (var i = array.length - 1; i >= 0; i--) { // if (array[i].name === undefined) { // var tempObject = {}; // for (var key in array[i]) { // tempObject.name = key; // tempObject.age = parseInt(array[i][key]) || 0; // if (!isNaN(tempObject.name)) { // tempObject.age = -1; // } // if (isNaN(tempObject.age)) { // tempObject.age = 0; // } // } // array[i] = tempObject; // } // } // }); function cleanRow(element, index, array) { if (element == null) { delete array[index]; return; } if (element.name == undefined) { element.name = Object.keys(element)[0]; } if (element.age == undefined) { element.age = element[element.name]; element.age = parseInt(element.age) || 0; } if (!isNaN(element.name)) { element.age = -1; } delete element[element.name]; } familyAgesPropName.forEach(cleanRow); console.log("familyAgesPropName"); console.log(familyAgesPropName); familyAgesWithoutPropName.forEach(cleanRow); console.log("familyAgesWithoutPropName"); console.log(familyAgesWithoutPropName); familyAgesWithoutPropNameMissingAge.forEach(cleanRow); console.log("familyAgesWithoutPropNameMissingAge"); console.log(familyAgesWithoutPropNameMissingAge); familyAgesWithoutPropNameMissingName.forEach(cleanRow); console.log("familyAgesWithoutPropNameMissingName"); console.log(familyAgesWithoutPropNameMissingName); familyAgesWithoutPropNameMissingNameAndNULL.forEach(cleanRow); console.log("familyAgesWithoutPropNameMissingNameAndNULL"); console.log(familyAgesWithoutPropNameMissingNameAndNULL); /** @brief: Manual implementation of the quicksort algorithm adapted our desired array, I've chosen do the algorithm manually because the sorting in JavaScript is very dependant on the implementation of the engine that runs the JavaScript making it erratic and not desirable to use. For example chrome V8 engine for JavaScript unstable. **/ var quickSort = (function() { function partition(array, left, right) { var cmp = array[right - 1].age, minEnd = left, maxEnd; for (maxEnd = left; maxEnd < right - 1; maxEnd += 1) { if (array[maxEnd].age <= cmp) { swap(array, maxEnd, minEnd); minEnd += 1; } } swap(array, minEnd, right - 1); return minEnd; } function swap(array, i, j) { var temp = array[i]; array[i] = array[j]; array[j] = temp; return array; } function quickSort(array, left, right) { if (left < right) { var p = partition(array, left, right); quickSort(array, left, p); quickSort(array, p + 1, right); } return array; } return function(array) { return quickSort(array, 0, array.length); }; }()); quickSort(familyAgesPropName); quickSort(familyAgesWithoutPropName); quickSort(familyAgesWithoutPropNameMissingAge); quickSort(familyAgesWithoutPropNameMissingName);
sort()
implementations are erratic, and that you will write your own improved sort routine sounds very uninformed. I would advise you not to write this type of comments in interview or school assignments.\$\endgroup\$