0
Object a1= "[{"rdf:resource":"record\/16777228-43386050"},{"rdf:resource":"record\/16777228-43386055"},{"rdf:resource":"record\/16777228-43386057"},{"rdf:resource":"record\/16777228-43386059"}]"

Object a2= "[{"rdf:resource":"record\/16777228-43386059"},{"rdf:resource":"record\/16777228-43386057"},{"rdf:resource":"record\/16777228-43386055"},{"rdf:resource":"record\/16777228-43386050"}]"

Above Objects a1 & a2 , both are same, only the order in which they retrieved is different.

How to compare the above objects in such a way, that should return true

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 1
    Are those strings? Arrays? JSON? This doesn't compile in Java (and neither in any other language that I know of). Can you give provide a [minimal, reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – Joachim Sauer Nov 26 '19 at 11:50
  • that code won't run, you are missing a lot of "\" before the ' " '. also this is json code... decode it and check for equality – Alberto Sinigaglia Nov 26 '19 at 11:51
  • its a JSONObject. And the above value is sample one, i have removed some values, since its confidential – Amsaveni Natarajan Nov 26 '19 at 11:51
  • Possible duplicate of [Is there a Java reflection utility to do a deep comparison of two objects?](https://stackoverflow.com/questions/1449001/is-there-a-java-reflection-utility-to-do-a-deep-comparison-of-two-objects) – Pedro Caires Nov 26 '19 at 11:54
  • As its presented in a current form its a string that inside can be treated as JSON... To clarify this point please add what is `a1.getClass().getName()` – Mark Bramnik Nov 26 '19 at 11:54
  • @MarkBramnik : JSONObject response="some response"; for( Object key: a1.keySet()){Object a1=response.get(key); This is how value of a1 is fetched – Amsaveni Natarajan Nov 26 '19 at 12:14
  • its still says nothing about the type of a1... – Mark Bramnik Nov 26 '19 at 12:17

1 Answers1

1

You can override override hashCode() and equals() method.

First of all, create a java class that will represent your json object like below class.

class JSONObject{
  private String rdfResource;
  // setter/getter

  //override hashCode() and equals() method

}

After that convert json array to java object array.

Finally convert java array object to json.

Abid Khan
  • 11
  • 3