Bargain23
Bargain23

Reputation: 1973

How to disable the automatic sorting of items alphabetically in Choices.js?

I have the following array:

options = [
  "Asset Management & Investment Funds",
  "Financial Institutions",
  "Life Sciences",
  "TMT",
  "Other"
]

Choices.js by default sorts it alphabetically, and Other ends up before TMT which is jarring for the user.

const $projectDropdown = new Choices($this[0], {
  placeholder: true,
  placeholderValue: 'Select a project'
})

I tried look into the sortFilter function, but it only specifies the method of sorting.

Upvotes: 2

Views: 4545

Answers (2)

Sookie Singh
Sookie Singh

Reputation: 1623

Default sorting is true but you can pass another property shouldSort.

const $projectDropdown = new Choices($this[0], {
  placeholder: true,
  placeholderValue: 'Select a project',
  shouldSort: false
})

Upvotes: 1

OliverRadini
OliverRadini

Reputation: 6467

It looks like you need the property shouldSort. The readme has details.

const $projectDropdown = new Choices($this[0], {
  placeholder: true,
  placeholderValue: 'Select a project'
  shouldSort: false,
})

Upvotes: 12

Related Questions