Skip to content
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

Docs: Missing reference for sort #545

Closed
jonnor opened this issue Nov 13, 2016 · 3 comments
Closed

Docs: Missing reference for sort #545

jonnor opened this issue Nov 13, 2016 · 3 comments
Assignees

Comments

@jonnor
Copy link
Contributor

jonnor commented Nov 13, 2016

It is used a couple of places in the examples (like view.eve), but nothing that explains what it is and how it can be used...

For instance, I'm interested in limiting a search to only match the last record. Can/should I use sort for this, and if so, how?

Can sort be used in other places than search?

@Cormac-Williams
Copy link
Contributor

I've seen sort used in commits to @browser in order to control the order of divs, and I've had no problem using other functions and expressions in commit and bind, as long as they are not affecting the query.

This works for selecting the last element after sorting on a field.

search
i = range[from:1 to:5]
commit
[#test count:i value:"Test {{5 - i}}"]

search
test = [#test]
ix = sort[value:test.value given:test]
ix = count[given:test]
commit @browser
[#div text:"{{test.value}}"]

Regards,
Cormac

@cmontella
Copy link
Contributor

cmontella commented Nov 15, 2016

I'm going to close this and reopen it in the docs repo. You can track it here: #545

I'll answer your question now and sort of use that as an interim sort doc until I can get something more substantial together.

Syntax

index = sort[value, direction, per]

Arguments

  • value - a list of attributes to sortt
  • direction (optional) - a list of dir like a thingections by which to sort the variables
  • per (optional) - an attribute by which to group the sorted attributes. The default is to sort

The output of the function is a set of indices, each of which map to an element of the sorted attribute. For example, if the value is ("A","C","D","B"), then index is (1,3,4,2).

Examples

We have #student records with grade (1 - 12), teacher, GPA (0.0 - 4.0) attributes. We can sort the students by grade:

search
  [#student name grade]
  index = sort[value: grade]

bind @browser
  [#div sort: index, text: "{{index}} - {{name}} {{grade}}"]

The browser handles the task of rendering the divs in the order specified by the sortattribute. Taking this further, we can choose the direction of the sortted set, whether "up" or "down". The default direction is "up" when none is specified.

search
  [#student name grade]
  index = sort[value: grade, direction: "down"]

bind @browser
  [#div sort: index, text: "{{index}} - {{name}} {{grade}}"]

You can also sort across multiple axes of a record. For instance, we can sort grade from 9 to 12, then sort by name from Z - A.

search
  [#student name grade]
  index = sort[value: (grade, name) , direction: ("up","down")]

bind @browser
  [#div sort: index, text: "{{index}} - {{name}} {{grade}}"]

This can be extended to sort any number of attributes

search
  [#student name grade teacher GPA]
  index = sort[value: (grade, teacher, name, GPA) , direction: ("up", "down", "up", "down")]

bind @browser
  [#div sort: index, text: "{{index}} - {{name}} {{grade}} {{teacher}} {{GPA}}"]

Finally, we can group sorted attributes with the per argument. Here you can see the difference between sorting by name then grade, and sorting by name per grade.

search
  [#student name grade]
  index = sort[value: name, per: grade]

bind @browser
  [#div sort: index, text: "{{index}} - {{name}} {{grade}}"]

When you sort per grade, then name is first grouped by grade, and each of those groups is then sorted. This is why index goes from 1-6 instead of 1-20 as in the other examples; Although there are still 20 elements in index, the maximum is 6 because no grade has more than 6 students. You might want to sort data this way in order to display it in a nested structure, such as this:

search
    [#student name grade teacher]
  index = sort[value: name, per: grade]


bind @browser
  [#div grade children: 
    [#h3 text: "Grade: {{grade}}"]
    [#div sort: index, text: "{{index}} {{name}}"]]

Commit some test data

commit
  [#student name: "Mach", grade: 9, teacher: "Mr. Black", GPA: "1.0"]
  [#student name: "Pok", grade: 9, teacher: "Mrs. Brown", GPA: "3.4"]
  [#student name: "Karima", grade: 9, teacher: "Mr. Black", GPA: "2.4"]
  [#student name: "Garth", grade: 9, teacher: "Mrs. Brown", GPA: "2.8"]
  [#student name: "Berta", grade: 9, teacher: "Mr. Black", GPA: "2.7"]
  [#student name: "Maxine", grade: 10, teacher: "Mr. Red", GPA: "4.0"]
  [#student name: "Gwyn", grade: 10, teacher: "Mrs. Blue", GPA: "2.5"]
  [#student name: "Ilse", grade: 10, teacher: "Mr. Red", GPA: "3.0"]
  [#student name: "Hobert", grade: 11, teacher: "Ms. Green", GPA: "3.2"]
  [#student name: "Arlean", grade: 10, teacher: "Mr. Red", GPA: "2.4"]
  [#student name: "Ty", grade: 10, teacher: "Mrs. Blue", GPA: "1.7"]
  [#student name: "Kermit", grade: 11, teacher: "Ms. Green", GPA: "2.9"]
  [#student name: "Cortez", grade: 11, teacher: "Mrs. Orange", GPA: "2.3"]
  [#student name: "Polly", grade: 11, teacher: "Ms. Green", GPA: "2.1"]
  [#student name: "Damion", grade: 12, teacher: "Mrs. Purple", GPA: "3.8"]
  [#student name: "Gretchen", grade: 12, teacher: "Mrs. Yellow", GPA: "2.8"]
  [#student name: "Octavio", grade: 12, teacher: "Mrs. Purple", GPA: "3.4"]
  [#student name: "Pa", grade: 12, teacher: "Mrs. Yellow", GPA: "3.5"]
  [#student name: "Elwanda", grade: 10, teacher: "Mrs. Blue", GPA: "1.3"]
  [#student name: "Guadalupe", grade: 11, teacher: "Mrs. Orange", GPA: "3.7"]

@cmontella
Copy link
Contributor

Okay it's in: witheve/docs#27

Please let me know if this makes sense. Sort is simple in the base case, but can get complicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants