Is is possible to sort the result of a query with one field ascending and one field descending?

lynnrogers
Tera Expert

I know that it is possible to sort on multiple fields by adding multiple orderBy clauses to a query but I need to sort one field ascending and one descending. If I add one orderByclause and one orderByDesc clause the second one is ignored.

Is there any way I can sort one field ascending and one descending?

1 ACCEPTED SOLUTION

Ash10Patil
Kilo Guru

Hi,

Yes, you can add orderBy and ordebydesc clause in gliderecord query.

Refer below, it is working as to sort incidents based on state and desc order for priority:

var gr = new GlideRecord('incident');
gr.orderBy('state'); // Ascending Order
gr.orderByDesc('priority'); // Ascending Order
//gr.addEncodedQuery(queryString);
gr.query();
while (gr.next()) {
gs.info(gr.number+" "+gr.state +" P:"+gr.priority);
}

 

----Result of above script------

                 INC Number   State(ASC)  Priority(DESC)

*** Script: INC0010001   1   P:5
*** Script: INC0010112 1 P:5
*** Script: INC0010111 1 P:5
*** Script: INC0000057 1 P:5
*** Script: INC0000039 1 P:5
*** Script: INC0000058 1 P:5
*** Script: INC0009009 1 P:4
*** Script: INC0007002 1 P:4
*** Script: INC0000046 1 P:3
*** Script: INC0009001 1 P:3
*** Script: INC0000059 1 P:3
*** Script: INC0007001 1 P:1
*** Script: INC0009005 1 P:1
*** Script: INC0000029 2 P:5
*** Script: INC0000020 2 P:5
*** Script: INC0000047 2 P:3
*** Script: INC0000037 2 P:3
*** Script: INC0000041 2 P:3
*** Script: INC0000019 2 P:2
*** Script: INC0000027 2 P:2
*** Script: INC0000049 2 P:2
*** Script: INC0000044 2 P:2
*** Script: INC0000051 2 P:1
*** Script: INC0000055 2 P:1
*** Script: INC0000003 2 P:1
*** Script: INC0000050 2 P:1
*** Script: INC0000052 2 P:1
*** Script: INC0000031 2 P:1
*** Script: INC0000053 2 P:1
*** Script: INC0000018 2 P:1
*** Script: INC0000015 2 P:1
*** Script: INC0000025 2 P:1

 

 

Thanks.

View solution in original post

8 REPLIES 8

Thanks Valarmathi. I have realised that the problem was due to the data in one of the fields I was using but this method could be useful in future.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Can you inform where you are trying this?

For server side it is allowed to use combination of orderBy and ordeByDesc

In client side GlideRecord this is not allowed

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks Ankur. It was in the server-side script of a portal widget but I found my problem was due to the data in one of the fields.

Abhijeet Khandr
Giga Expert

Hello Lynnrogers,

 

 

By double clicking you can only use one column to sort ,however the Add Sort button on top of the list view can be used to add multiple sort conditions (ascending or Descdending) as in below screenshot

 

find_real_file.png

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

 

Regards,
Abhijeet Khandre