1

I have got a list of Customers (Image1- unsorted) and displaying this list in a table. Users able to sort this table by clicking table header.

If they click Customer Name the first time (Image2), the List is sorted by Customer Name from A-Z Then they click the Customer Name a second time (Image3), List sorted by Customer Name from Z-A

The issue is, when the user clicks the Customer Name the third time (Image4), I was expecting to see the list reordered like the first time(Image 2). But it is not. The list is ordered A-Z, but it is not in the same order As you can see from the images i attached Second Image and 3rd image orders are not the same order.

if (this.orderByColSide)
   authList.sort((a, b) => a.customerCode.toLowerCase() > b.customerCode.toLowerCase() ? 1 : -1);
else
   authList.sort((a, b) => a.customerCode.toLowerCase() > b.customerCode.toLowerCase() ? -1 : 1);            

UnSorted

Sort by Customer

Sort Customer again

Sort Customer again

10
  • 5
    You're not handling what happens if two things are equal. Commented Nov 30, 2022 at 10:36
  • "when the user clicks the Customer Name the third time (Image4), I was expecting to see the list reordered like the first time(Image 2). But it is not." is there any such functionality in whatever you use to show you this table? It seems to toggle between sorting ascending and descending. Can it even handle removing the sorting? And how does it even work? There is not a lot to go on in this post - you're showing some sorting that will sort in ascending or descending order. No idea what is using nor how to remove the sort (if possible). Commented Nov 30, 2022 at 10:39
  • Hi VLAZ, thank you for answer but as you can see from image 2 and 4, they are not equl. Look at the Deal Coloumn, You can see the diffrence on Deal Coloumn Commented Nov 30, 2022 at 10:45
  • Then that's going back to my first comment - you're not handling equality. Commented Nov 30, 2022 at 10:53
  • 1
    Hi Slebetman. Do you mean if (this.orderByColSide){ authList.sort((a, b) => a.customerCode.localeCompare(b.customerCode));} else { authList.sort((a, b) => b.customerCode.localeCompare(a.customerCode)); } Commented Nov 30, 2022 at 11:06

2 Answers 2

1

Use localeCompare for strings

if (this.orderByColSide)
   authList.sort((a, b) => a.customerCode.toLowerCase().localCompare(b.customerCode.toLowerCase())
else
   authList.sort((a, b) => - a.customerCode.toLowerCase().localeCompare(b.customerCode.toLowerCase())
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Steven and Slebetman. LocalCompare fixed the issue but I am not sure this is the best solution. I sort it with adding Equality into my compare, like VLAZ suggested.
The size of the integer returned from sort does not matter.
0
if (this.orderByColSide)
    authList.sort((a, b) => a.customerCode.toLowerCase() > b.customerCode.toLowerCase() ? 1 : a.customerCode.toLowerCase() < b.customerCode.toLowerCase()? -1: 0);                            
 else
     authList.sort((a, b) => a.customerCode.toLowerCase() > b.customerCode.toLowerCase() ? -1 : a.customerCode.toLowerCase() < b.customerCode.toLowerCase()? 1:0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.