https://github.com/yankouskia/sorter
https://github.com/yankouskia/sorter
Last synced: 20 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/yankouskia/sorter
- Owner: yankouskia
- Created: 2018-02-16T09:19:54.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-21T13:37:03.000Z (over 6 years ago)
- Last Synced: 2025-03-29T08:08:55.980Z (27 days ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 4
- Watchers: 1
- Forks: 768
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sorter
## Task
Implement class `Sorter` with next methods:
`constructor` - creates instance of `Sorter` class
`add` - add element and store it in any way inside
`length` - return the count of current elements, which were added to `Sorter` instance via `add` method
`toArray` - return all elements in array. For example:
```js
const sorter = new Sorter();
sorter.add(1);
sorter.add(2);console.log(sorter.length) // 2
console.log(sorter.toArray()) // [1, 2]
````sort` - takes indices of already added elements and sorts *only* these elements. For example:
```js
const sorter = new Sorter();
sorter.add(2);
sorter.add(1);console.log(sorter.length) // 2
console.log(sorter.toArray()) // [2, 1]sorter.sort([0, 1]);
console.log(sorter.toArray()) // [1, 2]
````setComparator` - takes `compareFunction` as parameter and use it while sorting elements. You can read about `compareFunction` [here](https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). **Sort should work for numbers by default (if compareFunction was not set directly using this method)** For example:
```js
const sorter = new Sorter();
sorter.add(2);
sorter.add(1);sorter.sort([0, 1]);
console.log(sorter.toArray()) // [1, 2]sorter.add(3);
const reverseCompareFunction = (left, right) => right - left;
sorter.setComparator(reverseCompareFunction);sorter.sort([0, 1, 2]);
console.log(sorter.toArray()); // [3, 2, 1]
```Write your code in `src/index.js.
*Be sure, that all tests are positive. That means you cannot catch any error in tests.*## Prepare and test
- Install [Node.js](https://nodejs.org/en/)
- Clone this repository: `git clone https://github.com/yankouskia/sorter.git`
- Go to folder `sorter`
- Run `npm install` in command line
- Run `npm test` in command line
- You will see the number of passing and failing tests