Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useSorted.md
1---2category: Array3---45# useSorted67reactive sort array89## Usage1011```ts12import { useSorted } from '@vueuse/core'1314// general sort15const source = [10, 3, 5, 7, 2, 1, 8, 6, 9, 4]16const sorted = useSorted(source)17console.log(sorted.value) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]18console.log(source) // [10, 3, 5, 7, 2, 1, 8, 6, 9, 4]1920// object sort21const objArr = [{22name: 'John',23age: 40,24}, {25name: 'Jane',26age: 20,27}, {28name: 'Joe',29age: 30,30}, {31name: 'Jenny',32age: 22,33}]34const objSorted = useSorted(objArr, (a, b) => a.age - b.age)35```3637### dirty mode3839dirty mode will change the source array.4041```ts42const source = ref([10, 3, 5, 7, 2, 1, 8, 6, 9, 4])43const sorted = useSorted(source, (a, b) => a - b, {44dirty: true,45})46console.log(source)// output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 1047```4849## Type Declarations5051```ts52export type UseSortedCompareFn<T = any> = (a: T, b: T) => number53export type UseSortedFn<T = any> = (54arr: T[],55compareFn: UseSortedCompareFn<T>,56) => T[]57export interface UseSortedOptions<T = any> {58/**59* sort algorithm60*/61sortFn?: UseSortedFn<T>62/**63* compare function64*/65compareFn?: UseSortedCompareFn<T>66/**67* change the value of the source array68* @default false69*/70dirty?: boolean71}72/**73* reactive sort array74*75* @see https://vueuse.org/useSorted76*/77export declare function useSorted<T = any>(78source: MaybeRefOrGetter<T[]>,79compareFn?: UseSortedCompareFn<T>,80): Ref<T[]>81export declare function useSorted<T = any>(82source: MaybeRefOrGetter<T[]>,83options?: UseSortedOptions<T>,84): Ref<T[]>85export declare function useSorted<T = any>(86source: MaybeRefOrGetter<T[]>,87compareFn?: UseSortedCompareFn<T>,88options?: Omit<UseSortedOptions<T>, "compareFn">,89): Ref<T[]>90```91