最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sorting 2 dimensional Javascript array - Stack Overflow

programmeradmin0浏览0评论

Can any one help me in sorting a 2 Dimensional Array

Which will have the data in the following format

[2, All are fine]
[4, All is Well]
[1, Welcome Code]
[9, Javascript]

After sorting it should look like 

[2, All are fine]
[4, All is Well]
[9, Javascript]
[1, Welcome Code]

Main thing that i am focussing is to sort based on Text not on the ID

Can any one help me in sorting a 2 Dimensional Array

Which will have the data in the following format

[2, All are fine]
[4, All is Well]
[1, Welcome Code]
[9, Javascript]

After sorting it should look like 

[2, All are fine]
[4, All is Well]
[9, Javascript]
[1, Welcome Code]

Main thing that i am focussing is to sort based on Text not on the ID

Share Improve this question asked Jun 27, 2011 at 8:33 gmhkgmhk 15.9k29 gold badges91 silver badges112 bronze badges 1
  • 2 possible duplicate of sort outer array based on values in inner array, javascript – Felix Kling Commented Jun 27, 2011 at 8:38
Add a comment  | 

3 Answers 3

Reset to default 10
ary.sort(function(a, b) { return (a[1] < b[1] ? -1 : (a[1] > b[1] ? 1 : 0)); });

See: http://jsfiddle.net/tdBWh/ for this example, and MDC for documentation.

You can use this kind of code :

function sortMultiDimensional(a,b)
{
    // for instance, this will sort the array using the second element    
    return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0));
}

and then use the sort method :

myArray.sort(sortMultiDimensional);

Regards,

Max

 ary.sort(function(x,y) { return x[1].localeCompare(y[1]) })
  • https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/localeCompare
  • http://msdn.microsoft.com/en-us/library/casyzyb5.aspx
发布评论

评论列表(0)

  1. 暂无评论