Reputation: 1
I need help for sorting the variable in JavaScript, below is my code. I want to sort surchargeTypeVariableList in the code. I am getting only one value in the front end. Actually it has 6 values but in the unordered list. I want make it in a ascending order.
_createSurcharges : function () {
var label, amount, sortedArray = [], displayPremiumHeader = false;
if (this.surchargeList && this.surchargeList.length > 0) {
array.forEach(this.surchargeList, lang.hitch(this, function (surcharge) {
debugger;
label = this.getContentItemLabelWithJurisdiction(this.policyContentPrefix + surcharge.displayKey, surcharge.displayKeyValue, this.jurisdiction);
if (surcharge.surchargeTypeVariableList && surcharge.surchargeTypeVariableList.length > 0) {
// Modifies Java based substitutions to work for dojo converts {x} to ${x}
label = this._modifyTemplate(label);
label = string.substitute(label, surcharge.surchargeTypeVariableList);
}
surcharge.label = label;
surcharge.surchargeTypeVariableList.sort(function (a, b) {
if (a.fieldType === label) {
return 1;
} else if (b.fieldType === label) {
return -1;
}
return 0;
});
if (surcharge.premium > 0) {
amount = currencies.reformat(surcharge.premium);
displayPremiumHeader = true;
}
}));
sortedArray = this.surchargeList.sort(this._sortByDisplayOrder);
amount = null;
domConstruct.place(string.substitute(this.col1Template, {
"label" : label
}), this.otherChargesListNode, "last");
if (amount) {
domConstruct.place(string.substitute(this.col2Template, {
"amount" : amount
}), this.otherChargesListNode, "last");
}
if (!displayPremiumHeader) {
this.set("premiumHeader", "");
}
} else {
domClass.add(this.domNode, "hide");
}
},
/**
* sort comparator<br/>
*
* @private
* @instance
*/
_sortByDisplayOrder : function (displayOrderable1, displayOrderable2) {
"use strict";
if (displayOrderable1.displayOrder === displayOrderable2.displayOrder) {
return 0;
}
return (displayOrderable1.displayOrder > displayOrderable2.displayOrder) ? 1 : -1;
},
/**
* Modifies Java based substitutions to work for dojo<br/>
* converts {x} to ${x}
* Note, there is a special case handling for formats such as $null <small>per accident</small>
* we assume a single substitution in this case. TODO verify this assumption
*
* @private
* @instance
*/
_modifyTemplate : function (template) {
var t = template;
if (template.indexOf("$null") !== -1) {
t = template.replace("$null", "${0}");
} else {
t = t.replace(/(\{[0-9]+\})/g, "$$$1");
}
return t;
}
});
});
Upvotes: 0
Views: 127
Reputation: 73938
In case surchargeTypeVariableList
is array of strings, consider using sort() without compare function.
In this way your will sort your array based on Unicode code point order.
Example:
var data = ['c', 'd', 'a', 'b'];
data.sort();
alert(data);
From your example:
surcharge.surchargeTypeVariableList.sort(function (a, b) {
if (a.fieldType === label) {
return 1;
} else if (b.fieldType === label) {
return -1;
}
return 0;
});
Change to:
surcharge.surchargeTypeVariableList.sort();
Notes:
Question does not provide an example of surchargeTypeVariableList
variable we can just guess on its type. Please update your question with more details and possibly with a minimal viable example.
Upvotes: 2