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

arrays - Javascript Case Insensitive Sort objects containing undefined as value - Stack Overflow

programmeradmin1浏览0评论

I have an array of objects

Say,

var fruits = [
   {name:'apple', capital:'sample'},
   {name:'Tomato', capital:'sample'},
   {name:'jack fruit', capital:'sample'},
   {name:undefined, capital:'sample'},
   {name:'onion', capital:'sample'},
   {name:'Mango', capital:'sample'},
   {name:'Banana', capital:'sample'},
   {name:'brinjal', capital:'sample'}
];

I need to sort the array in ascending by name

  1. The object may contain undefined in the name
  2. The object name may be a mixture of upper case and lowercase (So it must be a case insensitive search)

If the array has undefined, then that object should be pushed to the end of the sorted list.

Expected output

var fruits = [
   {name:'apple', capital:'sample'},
   {name:'Banana', capital:'sample'},
   {name:'brinjal', capital:'sample'},
   {name:'jack fruit', capital:'sample'},
   {name:'Mango', capital:'sample'},
   {name:'onion', capital:'sample'},
   {name:'Tomato', capital:'sample'},
   {name:undefined, capital:'sample'}
];

I have an array of objects

Say,

var fruits = [
   {name:'apple', capital:'sample'},
   {name:'Tomato', capital:'sample'},
   {name:'jack fruit', capital:'sample'},
   {name:undefined, capital:'sample'},
   {name:'onion', capital:'sample'},
   {name:'Mango', capital:'sample'},
   {name:'Banana', capital:'sample'},
   {name:'brinjal', capital:'sample'}
];

I need to sort the array in ascending by name

  1. The object may contain undefined in the name
  2. The object name may be a mixture of upper case and lowercase (So it must be a case insensitive search)

If the array has undefined, then that object should be pushed to the end of the sorted list.

Expected output

var fruits = [
   {name:'apple', capital:'sample'},
   {name:'Banana', capital:'sample'},
   {name:'brinjal', capital:'sample'},
   {name:'jack fruit', capital:'sample'},
   {name:'Mango', capital:'sample'},
   {name:'onion', capital:'sample'},
   {name:'Tomato', capital:'sample'},
   {name:undefined, capital:'sample'}
];
Share Improve this question asked Nov 20, 2014 at 9:48 nijinnijin 5562 silver badges16 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

const fruits = [
   { name: 'apple', capital: 'sample' },
   { name: 'Tomato', capital: 'sample' },
   { name: 'jack fruit', capital: 'sample' },
   { name: undefined, capital: 'sample' },
   { name: undefined, capital: 'sample' },
   { name: undefined, capital: 'sample' },
   { name: 'onion', capital: 'sample' },
   { name: 'Mango', capital: 'sample' },
   { name: 'Banana', capital: 'sample' },
   { name: 'brinjal', capital: 'sample' }
];

const res = fruits.sort(function (a, b) {
  if (a.name === undefined) return 1;
  if (b.name === undefined) return -1;
  if (a.name === b.name) return 0;
  return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
});

console.log(res);

发布评论

评论列表(0)

  1. 暂无评论