Skip to content

Instantly share code, notes, and snippets.

@lauri-kaariainen
Created April 6, 2014 12:06
Show Gist options
  • Save lauri-kaariainen/10005055 to your computer and use it in GitHub Desktop.
Save lauri-kaariainen/10005055 to your computer and use it in GitHub Desktop.
Javascript sort function for some mixed type (string, number, undefined) arrays
function simpleAnalyze(a,b){
if(a === "" && b !== "")
return -1;
if(a !== "" && b === "")
return 1;
if(a == b)
return 0;
return "";
}
//function for sorting array that has one of these values inside the objects: a[sortterm] => "609", 609, "", "hemingway"
function sortMixedArray(array, sortterm){
console.log("sorting with >"+sortterm+"<");
array.sort(function(a,b){
//console.log(typeof a[sortterm] + " and " + typeof b[sortterm]);
if(a[sortterm] != undefined && b[sortterm] != undefined){
//both strings, empty or not
if(typeof a[sortterm] === typeof b[sortterm] && typeof a[sortterm] === "string"){
//are they empty?
if(typeof simpleAnalyze(a[sortterm],b[sortterm]) === "number"){
//console.log("simpleAnalyze returned a number, hence one of the fields was '': "+simpleAnalyze(a[sortterm],b[sortterm]));
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: "+simpleAnalyze(a[sortterm],b[sortterm]));
return simpleAnalyze(a[sortterm],b[sortterm]);
}
//simpleanalyze didn't solve case, let's check out if both are numbers in string
else{
if(a[sortterm].match(/\d+[.,]?\d*/) && b[sortterm].match(/\d+[.,]?\d*/)){
//console.log("fields looked like numbers in strings");
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: "+((a[sortterm]).replace(",",".") - parseFloat((b[sortterm]).replace(",","."))));
return parseFloat((a[sortterm]).replace(",",".")) - parseFloat((b[sortterm]).replace(",","."));
}
//only one of the strings look like number, it should win
if(a[sortterm].match(/\d+[.,]?\d*/) || b[sortterm].match(/\d+[.,]?\d*/)){
//console.log("fields looked like numbers in one of the strings");
if(a[sortterm].match(/\d+[.,]?\d*/)){
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: 1");
return 1;
}
else{
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: 1");
return -1;
}
}
//normal strings, i guess
else {
//console.log("normal strings");
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: "+(a[sortterm].localeCompare(b[sortterm])));
return a[sortterm].localeCompare(b[sortterm]);
}
}
}
//both numbers
if(typeof a[sortterm] === typeof b[sortterm] && typeof a[sortterm] === "number"){
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: "+(a[sortterm] - b[sortterm]));
return a[sortterm] - b[sortterm];
}
//of different typeofs
//check if one looks like number while other is a number
if(typeof a[sortterm] === "number" && typeof b[sortterm] === "string"){
//b looks like a number
if(b[sortterm].match(/\d+[.,]?\d*/)){
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: "+(a[sortterm] - parseFloat((b[sortterm]).replace(",","."))));
return a[sortterm] - parseFloat((b[sortterm]).replace(",","."));
}
//the random text wins in this case if it is'nt empty
else {
if(typeof simpleAnalyze(a[sortterm],b[sortterm]) === "number"){
//console.log("simpleAnalyze returned a number, hence one of the fields was '': "+simpleAnalyze(a[sortterm],b[sortterm]));
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: "+simpleAnalyze(a[sortterm],b[sortterm]));
return simpleAnalyze(a[sortterm],b[sortterm]);
}
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: -1");
return +1;
}
}
if(typeof b[sortterm] === "number" && typeof a[sortterm] === "string"){
//a looks like a number
if(a[sortterm].match(/\d+[.,]?\d*/)){
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: "+(parseFloat((a[sortterm]).replace(",",".")) - b[sortterm]));
return parseFloat((a[sortterm]).replace(",",".")) - b[sortterm];
}
//text wins in this case too if it isn't empty
else{
if(typeof simpleAnalyze(a[sortterm],b[sortterm]) === "number"){
//console.log("simpleAnalyze returned a number, hence one of the fields was '': "+simpleAnalyze(a[sortterm],b[sortterm]));
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: "+simpleAnalyze(a[sortterm],b[sortterm]));
return simpleAnalyze(a[sortterm],b[sortterm]);
}
//console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: -1");
return -1;
}
}
console.warn("they were similar but in a way I couldn't see: " +typeof a[sortterm] + " and " + typeof b[sortterm] + ":'"+a[sortterm] + "' '" +b[sortterm]+"'");
return 0;
}
if(a[sortterm] === undefined || b[sortterm] !== undefined){
console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: -1");
console.log(array.indexOf(a));
return -1;
}
if(b[sortterm] === undefined || a[sortterm] !== undefined){
console.log("'"+a[sortterm] +"' vs. '"+b[sortterm]+"' winner is: +1");
console.log(array.indexOf(b));
return +1;
}
//they were undefined
else{
console.log("they were undefined");
return 0;
}
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment