私はJavaScriptで変数をソートするのに助けが必要です。以下は私のコードです。コードでsurchargeTypeVariableListを並べ替える必要があります。私はフロントエンドでただ一つの価値しか得ていません。実際には6つの値がありますが、順序付けられていないリストにあります。私は昇順にしたい。場合surchargeTypeVariableList
にJavaScriptでリストをソートする方法は?
_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;
}
});
});
コードをリファクタリングして、変数をソートするコードの部分だけを入れてください。ところで、どのタイプの変数ですか? 'Array'の場合の配列プロトタイプに' sort'メソッドがあります –