Company logo
April 29, 2021

Add sort functions to records for custom result orders

Vinay Ayyala's avatar

You can now add a sort function to any record via addContext. Let's check out this example below to see how sort functions work:

window.CommandBar.addContext("records", records, // records numbered 1 through 1000 { searchOptions: { sortFunction: (a: any, b: any) => a.id - b.id, // ascending }, });

Assume our "records" object looks like this:

records = [
  { name: 'Record 1', id: 1 },
  { name: 'Record 2', id: 2 },
  ...
  { name: 'Record 1000', id: 1000 }
]

When a user searches for a record (either via Quickfind or in an argument), CommandBar will:

  • Filter for records that match the user's query
  • Sort the remaining records using the provided search function

You can use custom sort functions to ensure users see relevant results at the top of their results list when searching. For example, maybe you want recently edited records to show up above older records, even if older records are a better match for the user's query.

Note that if you provide a custom searchFunction for your record, then a sortFunction won't be used.

How can I turn this on? 🛠

Add a sort function to your records in context as illustrated in the first code block.