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

proposal: useSort #1464

Closed
4 tasks done
okxiaoliang4 opened this issue Mar 28, 2022 · 5 comments · Fixed by #1799
Closed
4 tasks done

proposal: useSort #1464

okxiaoliang4 opened this issue Mar 28, 2022 · 5 comments · Fixed by #1799
Labels
enhancement New feature or request

Comments

@okxiaoliang4
Copy link
Member

Clear and concise description of the problem

Composable reactive sort array.

Suggested solution

// useSort/index.ts
import { computed } from '@vue/reactivity'
import type { MaybeRef } from '@vueuse/shared'
import { unref } from 'vue-demi'

export type UseSortCompareFn<T> = (a: T, b: T) => number

export type UseSortFn<T> = (arr: T[], compareFn: UseSortCompareFn<T>) => T[]

const defaultCompare: UseSortCompareFn<number> = (a, b) => a - b

export interface UseSortOptions<T> {
  compareFn?: UseSortCompareFn<T>
}

export function useSort<T>(arr: MaybeRef<T[]>, sortFn: UseSortFn<T>, options: UseSortOptions<T> = {}) {
  const { compareFn = defaultCompare } = options
  return computed(() => sortFn(unref(arr), compareFn as UseSortCompareFn<T>))
}
// useQuickSort/index.ts
import type { MaybeRef } from '@vueuse/shared'
import type { UseSortCompareFn, UseSortOptions } from '../useSort'
import { useSort } from '../useSort'

export function quickSort<T>(arr: T[], compareFn: UseSortCompareFn<T>): T[] {
  // sorted
  if (arr.length <= 1) return arr

  const pivot = arr[0]
  const centerArr: T[] = [pivot]
  const left: T[] = []
  const right: T[] = []

  for (let i = 1; i < arr.length; i++) {
    const element = arr[i]
    const compare = compareFn(element, pivot)
    if (compare === 0)
      centerArr.push(element)
    else if (compare < 0)
      left.push(element)
    else
      right.push(element)
  }

  return quickSort(left, compareFn).concat(centerArr, quickSort(right, compareFn))
}

export function useQuickSort<T>(arr: MaybeRef<T[]>, options: UseSortOptions<T> = {}) {
  return useSort(arr, quickSort, options)
}

Alternative

No response

Additional context

No response

Validations

@okxiaoliang4 okxiaoliang4 added the enhancement New feature or request label Mar 28, 2022
@mdbetancourt
Copy link
Contributor

mdbetancourt commented Mar 29, 2022

why not just use one function with algorithm as an option

useSort(arr, { algorithm: quicksort })

@wheatjs
Copy link
Member

wheatjs commented Mar 29, 2022

I'd prefer the initial implementation. I think it is more composable that way, similar to how we have useStorage, useLocalStorage, and useSessionStorage

@mdbetancourt
Copy link
Contributor

I'd prefer the initial implementation. I think it is more composable that way, similar to how we have useStorage, useLocalStorage, and useSessionStorage

we have only a few storage options but sort algorithms.. there is at least 40+ different options

@wheatjs
Copy link
Member

wheatjs commented Mar 30, 2022

Sure but the current implementation supports providing a custom algorithm in useSort as the second parameter. Seems unlikely that we would end up providing the 40+ different algorithms. I think providing the base function along with a few common sorting algorithms is the best option.

@okxiaoliang4 okxiaoliang4 linked a pull request Jul 8, 2022 that will close this issue
9 tasks
@yjl9903 yjl9903 mentioned this issue Jul 12, 2022
13 tasks
@stale
Copy link

stale bot commented Sep 14, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 14, 2022
@stale stale bot closed this as completed Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
3 participants