SKL
SKL

Reputation: 1453

Typescript - Rearrange specific object

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

Upvotes: 0

Views: 457

Answers (2)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48640

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; }

Upvotes: 0

HMR
HMR

Reputation: 39280

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
    );

Upvotes: 1

Related Questions