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

feat(useSort, useQuickSort): new function #1465

Closed
wants to merge 45 commits into from

Conversation

okxiaoliang4
Copy link
Member

@okxiaoliang4 okxiaoliang4 commented Mar 28, 2022

Description

Composable reactive sort array.

If this PR passes the merge, I will start adding other common sorting algorithms.

Additional context

closes: #1464


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

Copy link
Member Author

@okxiaoliang4 okxiaoliang4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self reviewed

@okxiaoliang4 okxiaoliang4 marked this pull request as ready for review March 28, 2022 07:39
* @param sortFn sort function
* @param options
*/
export function useSort<T>(source: MaybeRef<T[]>, sortFn: UseSortFn<T>, options: UseSortOptions<T> = {}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function useSort<T>(source: MaybeRef<T[]>, sortFn: UseSortFn<T>, options: UseSortOptions<T> = {}) {
export function useSorted<T>(source: MaybeRef<T[]>, sortFn: UseSortFn<T>, options: UseSortOptions<T> = {}) {

sort sounds like mutating, while we are trying to be immutable. sorted sounds like a better fit https://github.com/tc39/proposal-change-array-by-copy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to see it matches with JS's sort more with an optional comparator (a, b) => number

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to see it matches with JS's sort more with an optional comparator (a, b) => number

Already have
https://github.com/vueuse/vueuse/pull/1465/files#diff-57d9b1ba0ff31d653b57c370d5eaf1e56421d821a8aa79aba512a3da23a2747dR13

import type { UseSortCompareFn } from '../useSort'
import { useSortWrapFn } from '../useSort'

export function quickSort<T>(source: T[], compareFn: UseSortCompareFn<T>): T[] {
Copy link
Member

@antfu antfu Mar 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit hesitated about this. Could we force on the general useSorted first and then think about the possible way to support more algorithms?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I feel JS's native sort should be quite fast already (https://stackoverflow.com/a/37245185), I have never written a sorting algorithm in JS actually. Is there any benefit to implementing a quick sort on our own?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit hesitated about this. Could we force on the general useSorted first and then think about the possible way to support more algorithms?

ok~.

Also, I feel JS's native sort should be quite fast already (https://stackoverflow.com/a/37245185), I have never written a sorting algorithm in JS actually. Is there any benefit to implementing a quick sort on our own?

Not sure. Every browser type has it's own javascript engine implementation.
Just wanted to provide a useSorted implementation here.

* @param sortFn sort function
* @param options
*/
export function useSort<T>(source: MaybeRef<T[]>, sortFn: UseSortFn<T>, options: UseSortOptions<T> = {}) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antfu Perhaps it would be better if I added sortFn to the options, providing a default sort method.

export interface UseSortedOptions<T> {
  /**
   * sort algorithm
   */
  sortFn?: UseSortedFn<T>
  /**
   * compare function
   */
  compareFn?: UseSortedCompareFn<T>
  /**
   * change the value of the source array
   * @default false
   */
  dirty?: boolean
}

const defaultSortFn = <T>(source: T[], compareFn: UseSortedCompareFn<T>): T[] => source.sort(compareFn)

export function useSorted<T>(source: MaybeRef<T[]>, options: UseSortedOptions<T> = {}) {
  const { sortFn = defaultSortFn, compareFn = defaultCompare, dirty = false } = options
  // ...
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@okxiaoliang4 okxiaoliang4 marked this pull request as draft April 21, 2022 09:48
@okxiaoliang4 okxiaoliang4 mentioned this pull request Jul 8, 2022
9 tasks
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

Successfully merging this pull request may close these issues.

proposal: useSort