無法在JavaScript中的多個字段上對對象進行排序(AngularJS)

[英]Can't sort objects on multiple fields in JavaScript (AngularJS)


EXAMPLE: http://jsfiddle.net/w38E8/4/

示例:http://jsfiddle.net/w38E8/4/

Please check out the above example.

請查看上面的示例。

CONTROLLER:

控制器:

function SortCtrl($scope) {
$scope.students = [{
    'name': 'AAA',
        'year': 'sophomore',
        'score': 100,
}, {
    'name': 'ABA',
        'year': 'freshman',
        'score': 70,
}, {
    'name': 'ABC',
        'year': 'freshman',
        'score': 30,
}, {
    'name': 'BAA',
        'year': 'junior',
        'score': 90,
}, {
    'name': 'BAB',
        'year': 'junior',
        'score': 70,
}, {
    'name': 'BBA',
        'year': 'junior',
        'score': 50,
}, {
    'name': 'CAA',
        'year': 'sophomore',
        'score': 30,
}, ];

$scope.sortArr = ['name']; // Sorts the students array by this                        

$scope.sortClass = function (field) { // asc:BLUE, desc:RED
    var fieldIdx = getIndexInArray(field, $scope.sortArr);
    if (fieldIdx > -1) {
        return $scope.sortArr[fieldIdx].indexOf('-') > -1 ? 'desc' : 'asc';
    }
    return '';
};

$scope.changeSort = function (field, $event) { // When the header is clicked
    var fieldIdx = getIndexInArray(field, $scope.sortArr);

    if (fieldIdx > -1) {
        if ($event.shiftKey) {

        } else if ($scope.sortArr.length > 1) {
            $scope.sortArr = [field];
            fieldIdx = getIndexInArray(field, $scope.sortArr);
        }
        $scope.sortArr[fieldIdx] = $scope.sortArr[fieldIdx].indexOf('-') > -1 ? $scope.sortArr[fieldIdx].replace('-', '') : '-' + field;
    } else {
        if ($event.shiftKey) {
            $scope.sortArr.push(field);
        } else {
            $scope.sortArr = [field];
        }
    }

    var length = $scope.sortArr.length;

    $scope.students.sort(function (a, b) {
        var sortA = '';
        var sortB = '';
        var fieldA = '';
        var fieldB = '';

        for (var i = 0; i < length; i++) {
            if (field == 'year') {
                if (field == $scope.sortArr[i].replace('-', '')) {
                    fieldA += customOrder(a.year);
                    fieldB += customOrder(b.year);
                } else {
                    sortA += customOrder(a.year);
                    sortB += customOrder(b.year);
                }
            } else {

                if (field == $scope.sortArr[i].replace('-', '')) {
                    fieldA += a[$scope.sortArr[i].replace('-', '')];
                    fieldB += b[$scope.sortArr[i].replace('-', '')];
                } else {
                    sortA += a[$scope.sortArr[i].replace('-', '')];
                    sortB += b[$scope.sortArr[i].replace('-', '')];
                }
            }
        }


        if (sortA != sortB) { // To sort multiple fields
            if (sortA < sortB) return -1;
            if (sortA > sortB) return 1;
            return 0;
        }

        if ($scope.sortArr[getIndexInArray(field, $scope.sortArr)].indexOf('-') > -1) return fieldA == fieldB ? 0 : (fieldA < fieldB ? 1 : -1);
        else return fieldA == fieldB ? 0 : (fieldA < fieldB ? -1 : 1);
    });
};



function getIndexInArray(field, arr) {
    var idx = -1;

    angular.forEach(arr, function (value, index) {
        if (field == value.replace('-', '')) {
            idx = index;
        }
    });
    return idx;
};

function customOrder(type) {
    switch (type) {
        case 'freshman':
            return 0;
        case 'sophomore':
            return 1;
        case 'junior':
            return 2;
        case 'senior':
            return 3;
    }
};
};

So the example shows that you can sort multiple fields with pressing the shift key. Also, when you click the header, it changes the order each time.

因此,該示例顯示您可以按shift鍵對多個字段進行排序。此外,當您單擊標題時,它每次都會更改順序。

The problem is I cannot sort the objects, YEAR:DESC, then name:ASC. What did I wrong? I am pretty sure this part is wrong:

問題是我無法對對象進行排序,YEAR:DESC,然后名稱:ASC。我錯了什么?我很確定這部分是錯的:

if (sortA != sortB) { // To sort multiple fields
    if (sortA < sortB) return -1;
    if (sortA > sortB) return 1;
    return 0;
}

I have no idea how should I fix this issue. Please help me out! I am stuck on this issue for a long time!

我不知道該如何解決這個問題。請幫幫我!我被困在這個問題很長一段時間了!

EXAMPLE: http://jsfiddle.net/w38E8/4/

示例:http://jsfiddle.net/w38E8/4/

1 个解决方案

#1


2  

I think I got it to work.

我想我得上班了。

function  ascF(a, b) { if(a < b) return -1; else if(a > b) return  1; else return 0;};
function descF(a, b) { if(a < b) return  1; else if(a > b) return -1; else return 0;};
var length = $scope.sortArr.length;

$scope.students.sort(function (a, b) {
    for (var i = 0; i < length; i++) {
        var field = $scope.sortArr[i],
            sortF = ascF;
        if(field.charAt(0) === "-") {
            sortF = descF; field = field.substring(1);
        }
        if(field === "group") field = "score";
        if(a[field] === b[field]) continue;
        if(field === "year") return sortF(customOrder(a[field]), customOrder(b[field]));
        else return sortF(a[field], b[field]);
    }
    return 0;
});

Try it here http://jsfiddle.net/9W3bC/

在這里試試http://jsfiddle.net/9W3bC/


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/08/05/72fa8ebfd6f188020004592a999f3474.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com