Comments (23)
It's certainly a useful behavior—but, unfortunately, object enumeration semantics are implementation-dependent, so there's no guarantee that your object properties will actually be sorted.
Update: That said, this is a backward-compatibility issue that merits further discussion. @jdalton, @mathiasbynens?
from lodash.
Perhaps I'm misunderstanding you, but sortBy
should still iterate through all of the properties in the input Object. In Underscore, sortBy
returns an array with elements ordered by an iterator function. It's been reliable for me in practice, and it would be great if Lo-Dash supported this as well.
from lodash.
@jeremyckahn Right, but I don't think it makes much sense to sort an object (given that the order of the properties isn't defined to begin with).
In other words: console.log(sorted)
may produce either [ { prop: 1 }, { prop: 2 } ]
or [ { prop: 2 }, { prop: 1 } ]
. Both results are equally valid according to the spec.
from lodash.
The sample code I posted in my original message produces consistent results in Chrome and Firefox, but it's worthwhile to put together a unit test to confirm the ordering with different interpreters. I can put one together in the next day or so.
Aside from that, I'll just reiterate that I feel that this is really useful functionality in Underscore, and I'd love it if Lo-Dash supported it.
from lodash.
@jeremyckahn Thanks! If the ordering turns out to be nothing more than an academic concern, we can definitely support this in Lo-Dash!
from lodash.
Here's a quick and dirty unit test. I get consistent results in Chrome, Firefox, and IE7/8.
<!DOCTYPE html>
<html>
<head>
<link href="http://rekapi.com/lib/shifty/tests/qunit/qunit.css" rel="stylesheet" type="text/css" />
<script src="http://rekapi.com/lib/shifty/tests/qunit/qunit.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
(function () {
module('Underscore sortBy ordering');
test('Objects are sorted into Arrays', function () {
var sorted = _.sortBy({
a: { prop: 2 },
b: { prop: 1 }
}, function (obj) {
return obj.prop;
});
equal(sorted[0].prop, 1, 'First property was sorted correctly.');
equal(sorted[1].prop, 2, 'Second property was sorted correctly.');
});
} ())
</script>
</head>
<body>
<h1 id="qunit-header">Underscore sortBy test</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-useragent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"></div>
</body>
</html>
from lodash.
Thanks for doing this, Jeremy—much appreciated!
from lodash.
Chrome and Opera sort numeric properties, which is inconsistent with other browsers:
http://jsbin.com/ejecor
@jeremyckahn I've tried to find a sweet spot between tested/documented/common use. I moved sortBy
and groupBy
to the "Arrays" category because its documented example and tested usage was for arrays.
from lodash.
@jeremyckahn
As more devs use Lo-Dash we will get a better feeling on how the API is actually used. I'm not against moving groupBy
and sortBy
back to the "Collections" category.
@kitcambridge
Object iteration issues seem like a separate concern from what groupBy
is doing as it's grouping and not sorting.
from lodash.
@jdalton I'm happy to throw in my two cents, as I feel that this is a really valuable project.
I'm curious, is there a significant performance concern with moving sortBy
into the Collections category?
from lodash.
I'm curious, is there a significant performance concern with moving sortBy into the Collections category
Naw, not really a big perf hit, it just keeps things simpler but it's easy enough to add back ;)
I'm curious do you use _.max
and _.min
with objects too?
from lodash.
I haven't, but I can see as much value in using max
and min
on Objects as I do with sortBy
.
from lodash.
What do you think of _.shuffle
supporting objects?
from lodash.
I think shuffle
is fine with just operating on Arrays. The order of the elements in an Object are already random, so making them a different kind of random doesn't add much to the library.
from lodash.
The code needed to get this to work requires adding more method calls, and dependencies. You should rethink your animation framework's approach. Iterating over object properties is a pretty lame way to go about it. That said, I am patching it.
from lodash.
You could also try _.pluck(collection, 'prop').sort()
.
from lodash.
I'm curious as to why you say "iterating over object properties is a pretty lame way to go about" calculating state for animation. Could you please elaborate?
from lodash.
@jeremyckahn Animation frameworks should be fast and iterating over objects via a for-in loop is pretty slow compared to other ways of iteration. I also think it's a bit odd that your animation framework relies on a utility lib for low level iteration.
from lodash.
@jdalton I agree that animation frameworks should fast, and performance optimization is certainly a high-priority goal of Rekapi. That being said, I don't know how else to compute animation state without inspecting each property and calculating the state from that.
As for relying on a utility library, it makes Rekapi's code far more concise and lets me avoid reinventing the wheel for many common functions. The performance has been great so far. Over-optimization leads to unmaintainable code.
from lodash.
Patched here.
from lodash.
That being said, I don't know how else to compute animation state without inspecting each property and calculating the state from that.
State data (like coordinates or color) can be stored in objects but lists like _actors
and _canvasActors
would fit better as arrays.
As for relying on a utility library, it makes Rekapi's code far more concise and lets me avoid reinventing the wheel for many common functions. The performance has been great so far. Over-optimization leads to unmaintainable code.
Ya I get that. As you've gotten a bit of a perf boost by replacing Underscore with Lo-Dash you might take it a step further in perf critical areas and rely on your own custom iteration code that is tailored more for your specific needs (couldn't hurt to look anyways).
from lodash.
As of Lo-Dash v0.8.2 _.max
, _.min
, and _.shuffle
work with objects.
from lodash.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from lodash.
Related Issues (20)
- Method `sumBy` returns string HOT 2
- _.isEqual() returns false when array elements order is changed HOT 6
- Merge mutates source object if nested property is a class instance
- Future compatibility with Node.js/npm HOT 1
- Allow Default Value for toNumber("problem", 0) HOT 8
- isBuffer from lodash-es doesn't work HOT 1
- curryRight is broken in list.map context HOT 1
- Avoiding Lodash version disclosure HOT 1
- multiple vulnerabilities that exist in lodash.findlast HOT 4
- Attempt structuredClone in cloneDeep before doing manual clone HOT 4
- _.trimStart not work with "/" char
- _.kebabCase and _.camelCase treat some digits as delimiters but not others HOT 1
- async attempt
- lodash-es outdated HOT 1
- feature request: Safe maths functions HOT 6
- Alias `_.tail` to `_.last` HOT 2
- _.deburr() - ț not convert to t HOT 3
- Typescript error HOT 7
- Discussion on Performance Issues of `trimStart` and `trimEnd`(关于 `trimStart` 和 `trimEnd` 性能问题的讨论) HOT 3
- array _.difference bug HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lodash.