-
Notifications
You must be signed in to change notification settings - Fork 56
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
Sort an array of object literals #21
Comments
On 02/26/2014 02:52 PM, Travis Paul wrote:
There is not a way to do this in the library currently, but I like the Micah |
We could do something like: WJESortArray(doc, compareCB) It might be useful to sort objects as well as arrays, though I'm not sure if that could/should use the same mechanism or have its own WJESortObject callback would just take the key names instead of an arbitrary WJElement. Somehow being able to use dirent's alphasort() would also be a neat trick, but I can't think of a way to do that (for arbitrary array sorting anyway) without limiting flexibility pretty severely. |
Below is what initially came to mind: void WJEArraySortByKey(WJElement array, char *key, WJEAction order); // {"myArray":[{"key":"A"},{"key":"B"},{"key":"C"}]}
WJEArraySortKey(WJEGet(doc, "myArray"), "key", WJE_ASCENDING); And maybe an accompanying flat array sort? void WJEArraySort(WJElement array, WJEAction order); // {"myArray":["C", "B", "A"]}
WJEArraySort(WJEGet(doc, "myArray"), WJE_DESCENDING) But that's just me thinking out loud. I like the idea of imitating JavaScript's Array.prototype.sort. |
On 02/26/2014 03:15 PM, penduin wrote:
I like this approach. The same function would work just fine for arrays and objects. typedef int (WJESortCB *)(WJElement a, WJElement b, void *data); If the object is an array then a->name and b->name will be NULL. If it |
Yeah, I like that. (Also duh, not sure why I got caught up on a separate one for objects :^) Including a couple of helpers for alphabetical sorting of strings/keys might be handy. Such a thing could even be the default, if NULL is passed for the callback. (Again emulating JS's array sort) |
On 02/26/2014 03:59 PM, penduin wrote:
Sounds good. Do you feel like writing it? :) |
If both of you are busy with other things I would be willing to create a branch and attempt to implement it. However, I am a C novice so you will probably need to yell at me until it looks good enough to merge in. It would take me longer to implement it than either of you, but it would be a great learning experience for me and I need the feature within the next couple of months anyways. |
On 02/27/2014 09:57 AM, Travis Paul wrote:
I would love that. I'm very happy to help with any questions about C you |
Thanks @minego, I really appreciate it! I will most likely break ground next weekend (March 8th) as I have some other things to finish up this weekend. |
Hi All, { } I tried through WJArray but it is returning junk values. |
On 10/01/14 01:08, primohan01 wrote:
Hi, You need to use the correct function to get the values as the C type WJElement last; l = NULL; The WJENumber() function will find the first element that matches the The _ version is used for finding multiple matches and takes an extra The final argument is the value to return if there is no match. I hope this helps. Please don't hesitate to ask if you have any other Micah |
Dear Micah, |
In my application I have the need to sort an array of object literals by a key, and the key can be any type. For example:
this...
should sort to to perhaps something like this...
I am not aware of any way to do this within the library itself but I can think of how to do it outside the library (looping over the array, sorting the keys and attaching the literals into a new array). I am curious if this use case is common enough that it would make sense being in the library itself, or if there was some clever way to use selectors?
The text was updated successfully, but these errors were encountered: