是否有可能快速排序基于其在阵列中键的对象,使用JavaScript? [英] Is it possible to quicksort objects based on their keys in an array, using JavaScript?

查看:142
本文介绍了是否有可能快速排序基于其在阵列中键的对象,使用JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要澄清的问题了一下,我有一个数组,数组中的一堆对象。我可以重新安排基于每个对象的关键值的数组中的对象?

当我试图这样做,它不断告诉我,变量(在这种情况下,学生)是不确定的。

当我使用内置的排序功能,它完美的作品。然而,这是一个作业,我要表现出快速排序功能的细分。

下面是code我使用的:

 函数swap(学生,firstIndex和secondIndex){
            变种临时=学生[firstIndex]
            学生[firstIndex] =学生[secondIndex]
            学生[secondIndex] =温度;
        }        功能分区(学生,左,右){            VAR支点=学生[Math.floor((右+左)/ 2)],
                我离开=,
                J =权利;
            而(ⅰ&下; = j)条{                而(学生[1] - ;透视){
                    我++;
                }                而(学生[J]>支点){
                    j--;
                }                如果(ⅰ&下; = j)条{
                    掉期(学生,I,J);
                    我++;
                    j--;
                }
            }            返回我;
        }        功能快速排序(学生,左,右){            VAR指标;            如果(student.length→1){                左= typeof运算离开!=数字? 0:左;
                右= typeof运算吧!=数字? student.length - 1:权利;                指数=分区(学生,左,右);
                如果(左<指数 - 1){
                    快速排序(学生,左,指数 - 1);
                }                如果(指数<右){
                    快速排序(学生,指数,权);
                }            }            返回的学生;
        }    VAR studentNumbersArray = []    变种randomArray = [];    对于(i = 0; I< studentsArray.length;我++){
        studentNumbersArray.push(studentsArray [I] .studentNumber);
    }
    功能sortStudentNumbers(){
        VAR学生= studentNumbersArray.name; //< - 这是我收到错误
        快速排序(学生);
        VAR updatedStudentArray = JSON.stringify(studentsArray);
        localStorage.setItem(生,updatedStudentArray);
        location.reload();
    }


解决方案

看到你的code几种排列后,我想你正在尝试做的需求看起来有点像这样。

 快速排序(arrayToSort,attributeToSortOn,左,右){
    ...
}
...
功能分区(arrayToSort,attributeToSortOn,左,右){
    VAR支点=学生[Math.floor((右+左)/ 2)] [attributeToSortOn]
    ...
    而(arrayToSort [I] [attributeToSortOn<透视){
    ...
}
...
快速排序(studentsArray,'studentNumber');

快速排序总是需要数组在每个位置值进行比较。你不能只是通过 studentsArray.studentNumber 因为要排序的属性是它自己的无用和数组没有类型之内它含有反正对象的知识。

To clarify a bit on the question, I have an array and within the array are a bunch of objects. Can I rearrange the objects in the array based on the key values of each object?

When I'm attempting to do so, it keeps telling me that the variable (in this case, students) is undefined.

When I use the built-in sort function, it works perfectly. However, this is a school assignment and I HAVE to show the breakdown of the quicksort function.

Here is the code I am using:

        function swap(student, firstIndex, secondIndex){
            var temp = student[firstIndex];
            student[firstIndex] = student[secondIndex];
            student[secondIndex] = temp;
        }

        function partition(student, left, right) {

            var pivot   = student[Math.floor((right + left) / 2)],
                i       = left,
                j       = right;


            while (i <= j) {

                while (student[i] < pivot) {
                    i++;
                }

                while (student[j] > pivot) {
                    j--;
                }

                if (i <= j) {
                    swap(student, i, j);
                    i++;
                    j--;
                }
            }

            return i;
        }

        function quickSort(student, left, right) {

            var index;

            if (student.length > 1) {

                left = typeof left != "number" ? 0 : left;
                right = typeof right != "number" ? student.length - 1 : right;

                index = partition(student, left, right);
                if (left < index - 1) {
                    quickSort(student, left, index - 1);
                }

                if (index < right) {
                    quickSort(student, index, right);
                }

            }

            return student;
        }

    var studentNumbersArray = []

    var randomArray = [];

    for (i = 0; i < studentsArray.length; i++) { 
        studentNumbersArray.push(studentsArray[i].studentNumber);              
    }


    function sortStudentNumbers() {    
        var student = studentNumbersArray.name; // <-- THIS IS WHERE I AM GETTING ERROR
        quickSort(student);
        var updatedStudentArray = JSON.stringify(studentsArray);
        localStorage.setItem("students", updatedStudentArray); 
        location.reload();
    }

解决方案

After seeing several permutations of your code I think what you are trying to do needs to look a little something like this.

quickSort(arrayToSort, attributeToSortOn, left, right) {
    ...
}
...
function partition(arrayToSort, attributeToSortOn, left, right) {
    var pivot = student[Math.floor((right + left) / 2)][attributeToSortOn]
    ...
    while (arrayToSort[i][attributeToSortOn] < pivot) {
    ...
}
...
quickSort(studentsArray, 'studentNumber');

quickSort always needs the array to compare values at each position. You can't just pass studentsArray.studentNumber because the attribute to sort on is useless on it's own and the array has no knowledge of the types of object contained within it anyway.

这篇关于是否有可能快速排序基于其在阵列中键的对象,使用JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆