按数组内容排序

发布于 2024-12-07 01:43:48 字数 576 浏览 0 评论 0原文

我有一个如下所示的数组:

markerArray[i] = [i, title, cat];

该数组位于添加动态内容的 for 循环内

,因此结果可能如下所示:

markerArray[0] = [0, A title, A];
markerArray[1] = [1, A title new, B];
markerArray[3] = [3, A title, E];
markerArray[4] = [4, A title, A];

所以我的问题是我如何或是否可以对数组进行排序,以便输出基于在 A、BC 等或标题或从数组内部获取的任何内容上,请注意最后一个(类别)现在按字母顺序排列:

    markerArray[0] = [0, A title, A];
    markerArray[1] = [4, A title, A];
    markerArray[2] = [1, A title new, B];
    markerArray[3] = [3, A title, E];

这可能吗?

I have an array looking like this:

markerArray[i] = [i, title, cat];

this array is within a for loop adding the content dynamic

so the result could be something like this:

markerArray[0] = [0, A title, A];
markerArray[1] = [1, A title new, B];
markerArray[3] = [3, A title, E];
markerArray[4] = [4, A title, A];

So my question is how can I or is it possible to sort the array so that the output would be based on A,B C etc or title or whatever taken from inside the array like this, note that the last (category) is in alphabetic order now:

    markerArray[0] = [0, A title, A];
    markerArray[1] = [4, A title, A];
    markerArray[2] = [1, A title new, B];
    markerArray[3] = [3, A title, E];

Is this even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

居里长安 2024-12-14 01:43:48

尝试 -

var markerArray = new Array();
markerArray[0] = [0, "A title", "A"];
markerArray[1] = [1, "A title new", "B"];
markerArray[3] = [3, "A title", "E"];
markerArray[4] = [4, "A title", "Z"];
markerArray[5] = [5, "A title", "A"];


markerArray.sort(function(a, b) {
   var comp1 = a[2];
   var comp2 = b[2];
   return (comp1 < comp2) ? -1 : (comp1 > comp2) ? 1 : 0;
})

alert(markerArray);

try -

var markerArray = new Array();
markerArray[0] = [0, "A title", "A"];
markerArray[1] = [1, "A title new", "B"];
markerArray[3] = [3, "A title", "E"];
markerArray[4] = [4, "A title", "Z"];
markerArray[5] = [5, "A title", "A"];


markerArray.sort(function(a, b) {
   var comp1 = a[2];
   var comp2 = b[2];
   return (comp1 < comp2) ? -1 : (comp1 > comp2) ? 1 : 0;
})

alert(markerArray);
不奢求什么 2024-12-14 01:43:48

您必须使用 Array 对象中提供的 sort 函数。
此函数/方法接受自定义用户函数来确定元素的排序方式。

function customSorter( a, b )
{
    var comp1 = a[2];
    var comp2 = b[2];
    return (comp1 < comp2) ? -1 : (comp1 > comp2) ? 1 : 0;
}
var markerArray = new Array();
markerArray[0] = [0, "A title", "A"];
markerArray[1] = [1, "A title new", "B"];
markerArray[3] = [3, "A title", "E"];
markerArray[4] = [4, "A title", "Z"];
markerArray[5] = [5, "A title", "A"];

markerArray.sort( customSorter );

有用信息: https://developer.mozilla.org/en/JavaScript/参考/Global_Objects/数组/排序

You have to use the sort function, available in the Array object.
This function/method accepts a custom user function to determine how the elements have to be sorted.

function customSorter( a, b )
{
    var comp1 = a[2];
    var comp2 = b[2];
    return (comp1 < comp2) ? -1 : (comp1 > comp2) ? 1 : 0;
}
var markerArray = new Array();
markerArray[0] = [0, "A title", "A"];
markerArray[1] = [1, "A title new", "B"];
markerArray[3] = [3, "A title", "E"];
markerArray[4] = [4, "A title", "Z"];
markerArray[5] = [5, "A title", "A"];

markerArray.sort( customSorter );

Useful info: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

醉梦枕江山 2024-12-14 01:43:48

使用自定义排序功能。阅读文档如何操作。

Use a custom sort function. Read a documentation how to do it.

丿*梦醉红颜 2024-12-14 01:43:48

您可以将比较函数作为参数传递给 Array.sort 方法,并将比较逻辑写入比较函数内。请参阅此处 https://developer.mozilla.org/en/JavaScript/Reference /Global_Objects/Array/sort

You can pass the compare function as a parameter to Array.sort method and write your comparison logic inside the compare function. See here https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

转身泪倾城 2024-12-14 01:43:48

您可以尝试这种方法:

   markerArray.sort(function(a,b){ 
      for (i = 0; i <= 3 ; i++) {
         if (a[i] <> b[i]) {
            if (a[i] < b[i]) return -1;
            if (a[i] > b[i]) return 1;
         }
      }
      return 0;
   });

这将尝试对第一个元素进行排序,如果它们相同,它将尝试对第二个元素进行排序,依此类推对第三个元素进行排序。

[编辑]添加一些有趣的链接(也请参阅其他答案):

You could try this kind of stuff:

   markerArray.sort(function(a,b){ 
      for (i = 0; i <= 3 ; i++) {
         if (a[i] <> b[i]) {
            if (a[i] < b[i]) return -1;
            if (a[i] > b[i]) return 1;
         }
      }
      return 0;
   });

This will try to sort with the first element, if they are identical, it tries to sort with the second one, and so on with the third one.

[EDIT] Adding some interesting links (see other answers too):

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文