OrderBy

 

By using OrderBy Dataweave function you Reorders the elements of an input using criteria that acts on selected elements of that input.

In this tutorial I will use the dataweave OrderBy function to sort the Employee as per the employee age

Input

{
  "status": "success",
  "data": [
    {
      "id": 1,
      "employee_name": "Tiger Nixon",
      "employee_salary": 320800,
      "employee_age": 61,
      "profile_image": ""
    },
    {
      "id": "2",
      "employee_name": "Garrett Winters",
      "employee_salary": 170750,
      "employee_age": 63,
      "profile_image": ""
    },
    {
      "id": "3",
      "employee_name": "Ashton Cox",
      "employee_salary": 86000,
      "employee_age": 66,
      "profile_image": ""
    },
    {
      "id": "4",
      "employee_name": "Cedric Kelly",
      "employee_salary": 433060,
      "employee_age": 22,
      "profile_image": ""
    },
    {
      "id": "5",
      "employee_name": "Airi Satou",
      "employee_salary": 162700,
      "employee_age": 33,
      "profile_image": ""
    },
    {
      "id": "6",
      "employee_name": "Brielle Williamson",
      "employee_salary": 372000,
      "employee_age": 61,
      "profile_image": ""
    }
  ]
}

Dataweave Expression

%dw 2.0
output application

---
payload.data map((item,index) ->
{
	"id":item.id,
	"employee_name":item.employee_name,
	"employee_salary":item.employee_salary,
	"employee_age":item.employee_age,
	"profile_image":item.profile_image
})  orderBy $.employee_age

Result

You can see the result in ascending order of employee age

[
	{
		"id": "4",
		"employee_name": "Cedric Kelly",
		"employee_salary": 433060,
		"employee_age": 22,
		"profile_image": ""
	},
	{
		"id": "5",
		"employee_name": "Airi Satou",
		"employee_salary": 162700,
		"employee_age": 33,
		"profile_image": ""
	},
	{
		"id": 1,
		"employee_name": "Tiger Nixon",
		"employee_salary": 320800,
		"employee_age": 61,
		"profile_image": ""
	},
	{
		"id": "6",
		"employee_name": "Brielle Williamson",
		"employee_salary": 372000,
		"employee_age": 61,
		"profile_image": ""
	},
	{
		"id": "2",
		"employee_name": "Garrett Winters",
		"employee_salary": 170750,
		"employee_age": 63,
		"profile_image": ""
	},
	{
		"id": "3",
		"employee_name": "Ashton Cox",
		"employee_salary": 86000,
		"employee_age": 66,
		"profile_image": ""
	}
]
  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Amit Ghorpade
Amit Ghorpade
3 years ago

Thanks for example. I am getting orderby field in query parameter. I am trying to do orderby $.attributes.queryParams.employee_age
but it is not working.
can you let me know how we can do orderby on dynamically passed field.
..Got the answer – $[attributes.queryParams.employee_age]

Last edited 3 years ago by Amit Ghorpade
Madava Rao
Madava Rao
3 years ago
Reply to  Amit Ghorpade

Hi Amit, you can use below code for dynamic sorting.

%dw 2.0
output application/json
var sort = “employee_salary” 

payload.data map((item,index) ->
{
  “id”:item.id,
  “employee_name”:item.employee_name,
  “employee_salary”:item.employee_salary,
  “employee_age”:item.employee_age,
  “profile_image”:item.profile_image
}) orderBy $[(sort)]

ARS
ARS
2 years ago

sort both employee age nd salary