首页 > 代码库 > jscode属性排序

jscode属性排序

根据data中的value 对geCoorMap 进行排序,暂定降序排序。

var data = http://www.mamicode.com/[{name:"name1",value:29},{name:"name2",value:34},{name:"name3",value:50}];

var geCoorMap = {‘name1‘:[123,2321],
‘name5‘:[1213,2321],
‘name2‘:[102,2321],
‘name3‘:[102,2321]
}
//排序


//根据Key来获取排序用的key
function getSortValue(key){

for (var index = 0; index < data.length; index++)
{

var tempData = http://www.mamicode.com/data[index];

if (tempData.name == key)

return tempData.value;
}

return 0;
}

 

function sortNumber(a,b)
{
return a.sortKey - b.sortKey
}

var msg = "" ;
var sortArray = [];
var sortArrayIndex = 0;
for (var Key in geCoorMap){
msg = msg+‘&‘+‘‘+Key+‘=‘+geCoorMap[Key]+‘‘;
sortArray[sortArrayIndex] = { name :Key ,data:geCoorMap[Key],sortKey: getSortValue(Key) };
sortArrayIndex++;
}

var sortEnd = sortArray.sort(sortNumber)
var geCoorMapSorted = {};
for (var index = 0; index < sortEnd.length; index++)
{
var mapItem = sortEnd[index];
geCoorMapSorted[mapItem.name] = mapItem.data ;
}

geCoorMapSorted

 

 

 

 


/* webpack 自动打包 */

jscode属性排序