0

I've arranged object in alphabetical order. I wanna move "Others" at last in dropdown. Is there any way to acheive this?

this.registrationMerchantService.getBusinessCategories().subscribe(response => {
  this.businessCategories = response;
  this.businessCategories.sort(function(a, b) {
    if (a.name < b.name) {
      return -1;
    }
    if (a.name > b.name) {
      return 1;
    }
    return 0;
  });
});

Json objects

export const BUSINESS_CATEGORIES: BusinessCategory[] = [
  { id: 1, name: 'Food & Beverage' },
  { id: 2, name: 'Transportation & Travel' },
  { id: 3, name: 'Fashion' },
  { id: 4, name: 'Leisure' },
  { id: 5, name: 'Digital Content' },
  { id: 6, name: 'Health and Beauty' },
  { id: 7, name: 'Online Shopping' },
  { id: 8, name: 'Telecommunications' },
  { id: 9, name: 'Utilities' },
  { id: 10, name: 'Others' },
  { id: 11, name: 'General' },
  { id: 12, name: 'Insurance' },
  { id: 13, name: 'Services' },
  { id: 14, name: 'Appliances' },
  { id: 15, name: 'Entertainment' },
  { id: 16, name: 'Household Goods & Groceries' },
];
Current result: enter image description here

Is there any idea to move "Others" at last in the dropdown list

SKL
  • 1,243
  • 4
  • 32
  • 53
  • There is no JSON in your question. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Apr 23 '18 at 16:19
  • What did you try? – Sergiu Paraschiv Apr 23 '18 at 16:22
  • Yes, it's entirely possible. Try to do it. **If** you get stuck, show your attempt, and people will be happy to help. – T.J. Crowder Apr 23 '18 at 16:22
  • I rearranged BUSINESS_CATEGORIES in alphabetical order. But i wanted to moved only "Others" at last in the dropdown list. I tried almost 4 hours, still couldn't figure out and facing difficulties to acheive this. That's why Im here for help. – SKL Apr 23 '18 at 16:29

2 Answers2

1

You can try sorting the array filtering out "Others" and then add it at the end:

    var categories = [
      { id: 1, name: 'Food & Beverage' },
      { id: 2, name: 'Transportation & Travel' },
      { id: 3, name: 'Fashion' },
      { id: 4, name: 'Leisure' },
      { id: 5, name: 'Digital Content' },
      { id: 6, name: 'Health and Beauty' },
      { id: 7, name: 'Online Shopping' },
      { id: 8, name: 'Telecommunications' },
      { id: 9, name: 'Utilities' },
      { id: 10, name: 'Others' },
      { id: 11, name: 'General' },
      { id: 12, name: 'Insurance' },
      { id: 13, name: 'Services' },
      { id: 14, name: 'Appliances' },
      { id: 15, name: 'Entertainment' },
      { id: 16, name: 'Household Goods & Groceries' },
    ];
    const sorted =   categories.sort(function(a, b) {
      if (a.name < b.name) {
        return -1;
      }
      if (a.name > b.name) {
        return 1;
      }
      return 0;
    });

    console.log(
      sorted
      .filter(x=>x.name!=="Others")//remove "Others"
      .concat(sorted.find(x=>x.name==="Others"))//add "Others" at the end
    );
HMR
  • 37,593
  • 24
  • 91
  • 160
0

You can create a function that accepts an ignore list, and you can return a sorting function that takes care of ignoring certain names, by placing them above or below in the list.

You can fiddle with the sorting direction to get the desired result.

var BUSINESS_CATEGORIES = [
  { id: 1,  name: 'Food & Beverage' },
  { id: 2,  name: 'Transportation & Travel' },
  { id: 3,  name: 'Fashion' },
  { id: 4,  name: 'Leisure' },
  { id: 5,  name: 'Digital Content' },
  { id: 6,  name: 'Health and Beauty' },
  { id: 7,  name: 'Online Shopping' },
  { id: 8,  name: 'Telecommunications' },
  { id: 9,  name: 'Utilities' },
  { id: 10, name: 'Others' },
  { id: 11, name: 'General' },
  { id: 12, name: 'Insurance' },
  { id: 13, name: 'Services' },
  { id: 14, name: 'Appliances' },
  { id: 15, name: 'Entertainment' },
  { id: 16, name: 'Household Goods & Groceries' },
];

let categories = this.BUSINESS_CATEGORIES.sort(customSort([ 'Others' ], 1));

console.log(JSON.stringify(categories, null, 2));

function customSort(ignoreList, direction) {
  ignoreList = ignoreList || [];
  direction  = direction  || 1;
  return function(a, b) {
    var aPos = ignoreList.indexOf(a.name);
    var bPos = ignoreList.indexOf(b.name);

    if (bPos > -1) return -1 * direction;
    if (aPos > -1) return 1 * direction;

    return a.name.localeCompare(b.name) * direction;
  }
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132