1
g.V().
has('organizationId', 'b121672e-8049-40cc-9f28-c62dff4cc2d9').

hasLabel('employee').
or
(
    has('firstName', containing('K')),
    has('lastName', containing('K')),
    has('department', containing('K'))
).
order().
by('lastName').
project('EmployeeId', 'FirstName', 'LastName','Status', 'EndDate', 'WorkLocation', 'LastHealthCheck', 'Nearby', 'Frequent', 'Department', 'ConfirmationDate' ).
by(id).
by('firstName').
by('lastName').
by('status').
by('endDate').
by('workLocation').
by('lastHealthCheckUp').
by('nearby').
by('frequent').
by('department').
by('confirmationDate')

The query is used to get employee details, but I want to perform sorting for all the fields dynamically, Eg: From UI Let say FirstName parameter is passed as ascending/Descending order then the query should perform ordering in ascending order or in descending order, I have performed sorting but query only accept ascending as a single parameter, I want my query to accept sorting parameters and perform sorting based on it for all the mentioned fields.

1 Answers1

1

One way to have a 'mode' is to use a formulation such as this. I used the air-routes data set to build this example. You could pass the value to your code using a withSideEffect or an inject step.

gremlin> g.inject('asc').as('mode').
......1>   V(1,2,3,4,5).
......2>   choose(select('mode').is('asc'),
......3>               order().by('city',asc),
......4>               order().by('city',desc)).
......5>   values('city')  
==>Anchorage
==>Atlanta
==>Austin
==>Boston
==>Nashville

gremlin> g.inject('desc').as('mode').
......1>   V(1,2,3,4,5).
......2>   choose(select('mode').is('asc'),
......3>               order().by('city',asc),
......4>               order().by('city',desc)).
......5>   values('city') 
==>Nashville
==>Boston
==>Austin
==>Atlanta
==>Anchorage   

EDITED to add:

You can also sort using an injected key name. The syntax with valueMap is necessary as simply doing by(select('key')) will not work.

gremlin>   g.inject('city').as('key').
......1>     V(1,2,3,4,5,6,7,8,9).
......2>     order().by(valueMap().select(select('key')).unfold()).
......3>     values('city')   

==>Anchorage
==>Atlanta
==>Austin
==>Baltimore
==>Boston
==>Dallas
==>Fort Lauderdale
==>Nashville
==>Washington D.C. 

FURTHER EDITED to add: Putting it all together you can go one step further and provide both the mode and the key to use for sorting to the query.

gremlin>  g.inject([key:'city',mode:'desc']).as('p').
......1>     V(1,2,3,4,5,6,7,8,9).
......2>     choose(select('p').select('mode').is('asc'),
......3>                 order().by(valueMap().select(select('p').select('key')).unfold()),
......4>                 order().by(valueMap().select(select('p').select('key')).unfold(),desc)).
......5>     values('city')

==>Washington D.C.
==>Nashville
==>Fort Lauderdale
==>Dallas
==>Boston
==>Baltimore
==>Austin
==>Atlanta
==>Anchorage          
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • Thanks for the quick solution Kelvin Lawrence, inject function is not supported azure cosmos Gremlin API and my application I am developing in azure cosmos Gremlin API, – sarfaraz shaikh Jun 16 '20 at 06:26