Simple 3-field sorting algorithm

After a year, people are stupid. I don't know if I change one question and three questions. The blog has not been updated for more than a month. Today, I will update an article on multi-field sorting of array objects.
Bold style sorting is nothing more than normal or reverse order. Let's talk about a positive sequence first. Then, of course, the reverse order is to reverse the 1 and -1 of the positive order.
Generally, most people will think of the **sort()** method in the case of sorting.
W3chool's introduction to sort is as follows: There is a note in sort that is the key to me writing this.
If no parameters are used when calling this method, the elements in the array will be sorted in alphabetical order. To be more precise, it is based on characters. The order of encoding is sorted. To achieve this, first convert all the elements of the array into strings (if necessary) for comparison.

If you want to sort according to other criteria, you need to provide a comparison function that compares two values ​​and then returns a number that describes the relative order of the two values. The comparison function should have two parameters a and b, and the return values ​​are as follows:

If a is less than b, a should appear before b in the sorted array, and a value less than 0 is returned.
If a is equal to b, 0 is returned.
If a is greater than b, a value greater than 0 is returned.

In order to facilitate our data, we will use the simplest 1,2,3. Take a look at my code. This method should not be the simplest and most efficient. If there is a simpler or easier one, please share it with me. Thank you guys:

var agentlists = 
[{
    
    "ORGCODE": "1", "SALEORGCODE": "1","SALECODE":"3"},
{
    
    "ORGCODE": "3", "SALEORGCODE": "2","SALECODE":"1"},
{
    
    "ORGCODE": "2", "SALEORGCODE": "3","SALECODE":"2"},
{
    
    "ORGCODE": "5", "SALEORGCODE": "2","SALECODE":"2"},
{
    
    "ORGCODE": "4", "SALEORGCODE": "1","SALECODE":"3"}];
//按照原机构ORGCODE第一序,原团队SALEORGCODE第二序,人员工号第三序SALECODE
agentlists.sort(function(a,b){
    
    
    if (a["ORGCODE"] === b["ORGCODE"]) {
    
    
        if(a['SALEORGCODE'] === b["SALEORGCODE"]){
    
    
            if (a["SALECODE"] > b["SALECODE"]) {
    
    
                return 1;
            } else if (a["SALECODE"] < b["SALECODE"]) {
    
    
                return - 1;
            } else {
    
    
                return 0;
            }
        }else {
    
    
            if (a["SALEORGCODE"] > b["SALEORGCODE"]) {
    
    
                return 1;
            } else {
    
    
                return - 1;
            }
        } 
    } else {
    
    
        if (a["ORGCODE"] > b["ORGCODE"]) {
    
    
            return 1;
        } else {
    
    
            return - 1;
        }
    }     
})
console.log(agentlists);

Of course, we will have different situations in our work. This is a positive sequence above me. If you want to change to the reverse order, you only need to swap the positions of 1 and -1. Maybe the code execution efficiency will be a bit slow.
If you have more fields, you need to **return 0;** continue recursion there.

Guess you like

Origin blog.csdn.net/lbchenxy/article/details/104426249