Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to make the selected item appear on top of the list instead of the bottom ? #294

Open
ghostlexly opened this issue Aug 5, 2024 · 2 comments

Comments

@ghostlexly
Copy link

When i open cmdk, the selected item appears at the end, how can we make it appear on top ?

Capture d’écran 2024-08-05 à 12 46 23

@lucarampi
Copy link

You can just filter the items to remove the selected ones and append the selected one to the beginning of the array.

Here is a ChatGPT example:

// Suppose you have an array of items and a selected item
const items: string[] = ['item1', 'item2', 'item3', 'item4'];
const selectedItem: string = 'item3';

// Function to filter and reorder the items
function filterAndReorderItems(items: string[], selectedItem: string): string[] {
  // Filter the items to remove the selected item
  const filteredItems = items.filter(item => item !== selectedItem);

  // Add the selected item to the beginning of the array
  const reorderedItems = [selectedItem, ...filteredItems];

  return reorderedItems;
}

// Call the function and log the result
const result = filterAndReorderItems(items, selectedItem);
console.log(result); // Output: ['item3', 'item1', 'item2', 'item4']

@ghostlexly
Copy link
Author

Yes i can do it like this, i was looking for a official solution in cmdk.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants