satish chennupati
satish chennupati

Reputation: 2650

compare two arrays in dataweave2.0

i want code to compare two arrays and determine if they are equal or not irrespective of their order

[a,b,c] compared to [a, b,c ] should be true [a,b,c] compare to [a,c,b] should be true as well.

i tried using the diff function from dataweave 2.0 but it works only if the parameters are Json objects not for arrays.

Upvotes: 1

Views: 3542

Answers (2)

machaval
machaval

Reputation: 5059

You can use the Diff module with the unordered property

import diff from dw::util::Diff
%dw 2.0
output application/json
---
{
  result: diff(payload.array, vars.array, {unordered: true}).matches
}

Upvotes: 2

satish chennupati
satish chennupati

Reputation: 2650

as @George mentioned a simple orderBy fixed my issue

import diff from dw::util::Diff
%dw 2.0
output application/json
---
{

  result: diff(payload.array orderBy  $, vars.array orderBy $).matches


}

fixed the issue.

Upvotes: 3

Related Questions