JavaScript typescript array sorting with random sort order typescript array sorting,typescript array sorting string,typescript array sort

I want to sort an array based on an sort order.

public array1: Array<string> = [];
array.push(some string values); //E.g dog,apple,man

I have a sort order like

  • dog = 1
  • apple = 3
  • man = 2

So i want to sort this array like dog,man,apple.

How to do this in typescript.

Answer:1

If you'd have your order in a map(object) you could just do this.

let list = ['dog', 'cat','apple'];
let sortOrder = {
    dog: 1,
    cat: 3,
    apple: 2
}

list.sort((a, b) => sortOrder[a] - sortOrder[b]);

You can see a working playground example here.

Answer:2

I suppose you use TypeScript (as tagged in you question).

let data = [
   {value:"dog", key:1},
   {value:"apple", key:3},
   {value:"man", key:2},
];

data.sort((d1, d2) => {
  if (d1.key > d2.key) return 1;
  if (d1.key < d2.key) return -1;
  return 0;
});

You have enhanced your question then this answer is not relevant anymore (and fixed code as mentioned in comments).

Answer:3

I've been trying to use Google charts with react. It works well, but I've moved the data from a local file to a mongoDB, which gives me a date-issue. With my test-data I added a correct data-format ...

I've been trying to use Google charts with react. It works well, but I've moved the data from a local file to a mongoDB, which gives me a date-issue. With my test-data I added a correct data-format ...

  1. google charts json example
  2. google charts json to datatable
  3. google charts json format
  4. google charts json data
  5. google charts json
  6. google charts json data javascript
  7. google charts json input example
  8. google charts json file
  9. google charts json data format
  10. google charts json php
  11. google charts json data source
  12. google charts json date
  13. google charts json input
  14. google charts json datetime
  15. google charts json ajax
  16. google charts using json data
  17. google charts arraytodatatable json
  18. google charts load json
  19. google charts timeline json
  20. google charts api json

Currently i have the following structure <OverlayTrigger trigger={["hover", "focus", "click"]} placement="bottom" overlay={( <Popover className="timeline-popover-container" id="tpc-1"&...

Currently i have the following structure <OverlayTrigger trigger={["hover", "focus", "click"]} placement="bottom" overlay={( <Popover className="timeline-popover-container" id="tpc-1"&...

  1. popover hide when click outside
  2. popover hide on click
  3. popover hide on body click
  4. popover hide on blur
  5. bootstrap popover hide when click outside
  6. hide popover on mouse leave
  7. hide popover on scroll
  8. hide popover on button click
  9. hide popover on mouse out
  10. hide popover on click inside
  11. popover on hide
  12. hide popover on drag

So I am building a drag and drop form builder and I want to make a particular field on all the inputs readOnly. This can be Select Dropdown Text input Number input Date input Date input All these ...

So I am building a drag and drop form builder and I want to make a particular field on all the inputs readOnly. This can be Select Dropdown Text input Number input Date input Date input All these ...

  1. make only inputs with type 'text'
  2. make a sentence with inputs

tl;dr: How can I chain onto Javascript's map() with my own function? Like - stuff.map(i => i.key).avg() where avg() is my own function to compute the average of the array returned by map? In ...

tl;dr: How can I chain onto Javascript's map() with my own function? Like - stuff.map(i => i.key).avg() where avg() is my own function to compute the average of the array returned by map? In ...