2

This is a sample array example as Input :

hashForAnimals = [{ 
    :animalCd=>"Tiger",:animalType=>"Carnivore", :sortOrder =>3},
    {:animalCd=>"Cow",:animalType=>"Herbivore", :sortOrder =>5},
    {:animalCd=>"Rabbit", :animalType=>"Herbivore", :sortOrder =>2}, 
    {:animalCd=>"Shark",:animalType=>"Carnivore", :sortOrder =>4}, 
    {:animalCd=>"Cow",:animalType=>"Carnivore", :sortOrder =>1},
    {:animalCd=>"Bear", :animalType=>"Omnivore", :sortOrder =>7},
    {:animalCd=>"Tiger",:animalType=>"Carnivore", :sortOrder =>6}]

Expected Output:

hashForAnimals = [{
  :animalCd=>"Cow", :animalType=>"Carnivore",  :sortOrder =>1},
  {:animalCd=>"Rabbit", :animalType=>"Herbivore", :sortOrder =>2},
  {:animalCd=>"Tiger",:animalType=>"Carnivore", :sortOrder =>3},
  {:animalCd=>"Shark",:animalType=>"Carnivore", :sortOrder =>4},
  :animalCd=>"Cow", :animalType=>"Herbivore",  :sortOrder =>5}
  {:animalCd=>"Bear", :animalType=>"Omnivore", :sortOrder =>7}]

I need to sort the array on the basis of sort order and then I need to make the array unique with respect to animalCd falling under the same group i.e. animalType.

Tiger has appeared once with the minimum sort order, however Cow is appearing twice in the array but with different animalType.

It can be done by collecting the array into multiple animalType arrays and sorting and making them unique and merge back to a single array. However, I need an elegant solution for this.

Sample code that I'm trying:

hashForAnimals.sort!{|x,y| x[:sortOrder].to_i<=>y[:sortOrder].to_i}.group_by { |a| a[:animalType]}

I can group them into seperate hashes but then I need to make the individual hashes unique and then merge into the array.

Is this even possible in the way by which I'm approaching it?

1 Answer 1

3

you can use sort_by method and uniq and values_at

hashForAnimals.sort_by{ |a| a[:sortOrder] }.uniq{ |k| k.values_at(:animalCd, :animalType) }


# => [{:animalCd=>"Cow", :animalType=>"Carnivore", :sortOrder=>1}, {:animalCd=>"Rabbit", :animalType=>"Herbivore", :sortOrder=>2}, {:animalCd=>"Tiger", :animalType=>"Carnivore", :sortOrder=>3}, {:animalCd=>"Shark", :animalType=>"Carnivore", :sortOrder=>4}, {:animalCd=>"Cow", :animalType=>"Herbivore", :sortOrder=>5}, {:animalCd=>"Bear", :animalType=>"Omnivore", :sortOrder=>7}]
Sign up to request clarification or add additional context in comments.

7 Comments

Did you try my last update, with values_at method ?
Was unaware of values_at API. Brilliant and thanks for the prompt reply!
There may be less to sort if you uniq before sort (and there's no reason to think uniq will be faster by doing it second).
What if i use uniq and delete element with more sortOrder ?
...but my suggestion does affect which elements are selected by uniq, which may be important (don't know from the question).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.