-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Add middle attributes sorting #120
Conversation
I've accidentally formatted things that I shouldn't on a formatter library :P Going to fix it right now! |
Please add a prettier config to the issue descriptions Currently I would expect that nothing would get sorted cause the default would be taken Also please note that the sorting isn't sorting in an alphabetical order in any way |
So, I'm thinking and experimenting things for long hours now, but I can't find an answer. What have I done since my first changes:
What I cannot understand is, why on earth This is the main problem, which I though I was solving. I kinda only refactored some code that was already working. 🤦🏻 |
Can you rename the PR title? :) And also you should merge master into your PR branch |
@SkyaTura Looking at the tests, it seems that your sort is not stable 🤔 |
Yes, but I don't know why. My knowledge is limited here, because I though it should work correctly. I'll study more about the sort method to understand what is going on, over this weekend, ( the last couple days have been busy for me :x ) |
Could it be the cases where the compare function returns Node's Some bits about this: https://bugs.chromium.org/p/v8/issues/detail?id=90 I would try a stable function stableSort(
array,
compare = (a, b) => a < b ? -1 : a > b ? 1 : 0
) {
const entries = array.map((value, index) => [value, index]);
entries.sort((a, b) => {
const order = compare(a[0], b[0]);
// When order is 0, sort by index to make the sort stable:
return order != 0 ? order : a[1] - b[1];
});
return entries.map(([value]) => value);
} Note that this doesn't modify |
Finally!!! My logic wasn't wrong after all 😅 Thank you a lot @lehni . This stableSort worked as a chamr. |
@SkyaTura @lehni Check out my newly added contributing.md 😃 |
@SkyaTura happy to hear that this helped! I was struggling with this situation a while back on Node also, it can be so confusing. |
return 0; | ||
} | ||
|
||
export function stableSort<T>(array: T[], compare: CompareFunction): any[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure, but isn't it : T[]
as return type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I'm new to TypeScript, I didn't knew how to properly type the method @lehni suggested.
Could you help me with this one?
It took a while to understand hahah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay, I can checkout your branch at evening and then suggest some types
TODO
Original post
Every time this code is saved it gets a different result:
Expected result