-3

I am looking to sort alpha numeric with lodash and written the below function, I am using common function for both type of sorts (sorting alphabets and sorting alphanumeric )

 import sortBy from 'lodash/sortBy';

 export const SecondaryQueryExecutor = ({ query, updateState, isJsonData }) => {
  const { loading, data, errorRedirect: error } = useSectionQuery(query, null, isJsonData);
  updateState(draft => {
    draft.loading = { ...draft.loading, [query.resultFieldName]: loading };
  });
  updateState(draft => {
    draft.queryResults = {
      ...draft.queryResults,
      [query.resultFieldName]: sortBy(data, o => o.name) // this is where I applied sorting
    };
  });
  return error || null;
};

but this sort looks like it is not working and it is sorting like this as below

enter image description here

Could any one please let me know how can I achieve alphanumeric sorting with those above mentioned values and need to tackle the alphabet sorting as well.

Many thanks in advance !!!

kumar jain
  • 93
  • 6
  • 2
    the array is not valid – Randy Casburn Nov 03 '20 at 19:49
  • 1
    1) Your screenshot (screenshots are discouraged on SO: please copy/paste *text*, where possible) shows "alphabetic sort" is working OK. 2) The values in the screenshot do *NOT* seem to match the values in your array. Why? 3) The array isn't even value JSON/Javascript. Why? 4) You can sort numerically, or you can sort lexicographically. Take your pick: but choose one or the other. – paulsm4 Nov 03 '20 at 19:49
  • 1
    What is the expected output? – Mr. Black Nov 03 '20 at 19:52
  • sorry for the misleading i will remove that array so that only screenshot is valid – kumar jain Nov 03 '20 at 19:52
  • Try - https://stackoverflow.com/a/4340339/409617 If your array is ["-2", "-1", "+/-10", "+/-12", "+1", "+2", "+10", "-10"]? – Mr. Black Nov 03 '20 at 19:54
  • @Mr.Black , looking to sort numerically and each value is appended with `"` at the end and sorting numerically – kumar jain Nov 03 '20 at 19:55
  • Try this solution ( stackoverflow.com/a/4340339/409617 ) with a little modification ["-2", "-1", "+/-10", "+/-12", "+1", "+2", "+10", "-10"].sort(sortAlphaNum).map(i => \`${i}"\`); – Mr. Black Nov 03 '20 at 20:02
  • @Mr.Black, thanks but i am using same function `SecondaryQueryExecutor ` for data array like normal strings, so in that case how can i handle – kumar jain Nov 03 '20 at 20:13
  • So do you want to sort by the numbers contained within the string? – Heretic Monkey Nov 03 '20 at 20:20
  • @HereticMonkey yeah, i am looking for the same – kumar jain Nov 03 '20 at 20:20
  • 2
    Does this answer your question? [How can i sort an array a certain way with characters and numbers in javascript?](https://stackoverflow.com/questions/18031468/how-can-i-sort-an-array-a-certain-way-with-characters-and-numbers-in-javascript) – Heretic Monkey Nov 03 '20 at 20:23

1 Answers1

1

If you want to sort numerically ... then you need to sort numbers, not text!

SUGGESTION:

  1. Store your values (numeric values!) in an array. EXAMPLE: [2,-1,4]
  2. Sort the array. EXAMPLE: [-1, 2, 4]
  3. Read the sorted array an element at a time and format the value. EXAMPLE: let s = '+/-' + a[i] + '"';
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • i cannot modify the array as it is coming from db like with `"` – kumar jain Nov 03 '20 at 20:07
  • 1
    Dude - you can't just take a string verbatim and magically sort it as a number. Heretic Monkey gave a good example [above](https://stackoverflow.com/a/18031577/421195). It uses a regex to extract the value, then sorts by value. Another approach is to parse out the numeric values as soon as you read them from the database, then store the numbers in the array (instead of the text). But you need to do *SOME* work on the "raw", apparently text data you're fetching from the database! – paulsm4 Nov 03 '20 at 21:35