Compare the upper and lower limits of element array sorting in js

Source: Internet
Author: User
When I encountered the problem of comparing the upper and lower thresholds of multiple records during work, the comparison program written in java was as follows: if (PartnerConstants. CODE_INFO_SUP_EVA_RESULT_TYPE_SCORE.equals (supEvaResultTypeCode) | PartnerConstants. CODE_INFO_SUP...

When I encountered the problem of comparing the upper and lower thresholds of multiple records, the comparison program written in java was as follows:
If (PartnerConstants. CODE_INFO_SUP_EVA_RESULT_TYPE_SCORE.equals (supEvaResultTypeCode)
| PartnerConstants. CODE_INFO_SUP_EVA_RESULT_TYPE_LEVEL.equals (supEvaResultTypeCode )){

// Sort by value offline
SortResults (supEvaSystemResults );
Boolean flag = false;
For (int I = 0; I // Deprecate the score
BigDecimal scoreLowerBefore = supEvaSystemResults. get (I). getScoreLower ();
// Score online
BigDecimal scoreLimitBefore = supEvaSystemResults. get (I). getScoreLimit ();
For (int j = I + 1; j BigDecimal scoreLowerAfter = supEvaSystemResults. get (j). getScoreLower ();
If (scoreLowerBefore. compareTo (scoreLowerAfter) = 0) {// 1) if the values of the first and last data values are equal, the conditions are not met.
Flag = true;
} Else if (scoreLimitBefore. compareTo (scoreLowerAfter)> = 0) {// 2) if the previous score goes online and goes offline with a value greater than or equal to the value of the next one, the conditions are not met.
Flag = true;
}
}
}
// The score range cannot be different.
If (flag)

/**
* Sort offline by value from small to large
* @ Param supEvaSystemResults
*/
Private void sortResults (List SupEvaSystemResults ){
Collections. sort (supEvaSystemResults, new Comparator (){
Public int compare (SupplierEvaluationSystemResultEntity r1, SupplierEvaluationSystemResultEntity r2)
After reading this idea, the general idea is to sort the upper or lower limit fields of the obtained object data, then, compare the for loop. If the lower limit of the first and second values is equal, the condition is not met. If the upper limit of the first value is greater than or equal to the lower limit of the last value, the condition is not met;
So I went online and found a way to sort the array elements by a field. The method is as follows:
// The by function accepts a member name string as the parameter // returns a comparison function var by = function (name) that can be used to sort the array of objects containing the member) {return function (o, p) {var a, B; if (typeof o =
"Object" & typeof p = "object" & o & p) {a = o [name]; B = p [name]; if (a = B) {return 0;} if (typeof a =
Typeof B) {return a <B? -1: 1;} return typeof a <typeof B? -1: 1 ;}else {throw ("error ");}}}
Then, use the. net Method to write the upper limit cross method as follows:
// Determine whether the upper and lower scores are cross
Crientity. sort (by ("scoreLower "));
Var criflag = true;
For (var m = 0; m {
// Deprecate the score
Var scoreLowerBefore = crientity [m]. scoreLower;
// Score online
Var scoreLimitBefore = crientity [m]. scoreLimit;
For (var j = m + 1; j Var scoreLowerAfter = crientity [j]. scoreLower;
If (parseFloat (scoreLowerBefore) = parseFloat (scoreLowerAfter) {// 1) if the scores of the first and last two data values are equal, the conditions are not met.
// ArrAlertMessage. push ("the upper and lower limit of the graph score cannot be crossed! ");
Criflag = false;
} Else if (parseFloat (scoreLimitBefore)> = parseFloat (scoreLowerAfter) {// 2) if the previous score is online and is greater than or equal to the value of the next one is offline, the conditions are not met.
// ArrAlertMessage. push ("the upper and lower limit of the graph score cannot be crossed! ");
Criflag = false;
}
}
}
If (! Criflag)
{
ArrAlertMessage. push ("the upper and lower limits of the graph score cannot be crossed! ");
}
Here is another method to solve this problem:
You do not need to sort the data and compare it directly. However, the lower limit of the previous data is greater than or equal to the lower limit of the next data or the lower limit of the next data is less than or equal to the upper limit of the previous data;
The lower limit of the previous data item is greater than or equal to the upper limit of the next data item or the upper limit of the next data item is less than or equal to the upper limit of the next data item;
This method is used by my colleagues, but I always feel that it is more likely that there will be problems if I do not sort it;
The upper and lower scores cannot be crossed:
// Confirm the price information
Function PriceClientClick (e)
{
RemoveGridAllEditControl ($ find ("<% = WgMaterialPrice. ClientID %> "));
Var objEntity = <% = wgMaterial. StrClientOprDetailName %>;
If (objEntity! = Null)
{
Var arrAlertMessage = [];
Var lstPrice = <% = WgMaterialPrice. StrClientOprEntityList %>;
If (lstPrice! = Null & lstPrice. length> 0)
{
For (var I = 0; I {

If (lstPrice [I]. minimumPurchases! = Null & lstPrice [I]. maximumPurchases! = Null)
{
If (parseInt (lstPrice [I]. minimumPurchases)> parseInt (lstPrice [I]. maximumPurchases ))
{
ArrAlertMessage. push ("supply price information: the" + (I + 1) + "Row purchase volume (from) cannot be greater than the purchase volume ()!
");
}
Else
{
For (var j = I + 1; j {
If (parseInt (lstPrice [j]. minimumPurchases) <= parseInt (lstPrice [j]. maximumPurchases ))
{
If (parseInt (lstPrice [I]. minimumPurchases) <= parseInt (lstPrice [j]. minimumPurchases) | parseInt (lstPrice [j]. minimumPurchases) <= parseInt (lstPrice
[I]. maximumPurchases ))
{
ArrAlertMessage. push ("supply price information: No." + (I + 1) + "line purchase volume and No." + (j + 1) + "cannot cross the purchase volume!
");
}
Else if (parseInt (lstPrice [I]. minimumPurchases) <= parseInt (lstPrice [j]. Limit) | parseInt (lstPrice [j]. maximumPurchases) <= parseInt (lstPrice
[I]. maximumPurchases ))
{
ArrAlertMessage. push ("supply price information: No." + (I + 1) + "line purchase volume and No." + (j + 1) + "cannot cross the purchase volume!
");
}
}
}
}
}

}
}

 
Author "zhangjingjing"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.