# Convert a array of string

**URL:** <https://www.ruby-forum.com/t/convert-a-array-of-string/259252>\
**Category:** Uncategorized\
**Created:** [April 15, 2020, 2:54pm UTC](https://www.ruby-forum.com/t/convert-a-array-of-string/259252 "2020-04-15T14:54:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChiragArya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aeb1de/32.png) [@ChiragArya](https://www.ruby-forum.com/u/ChiragArya)\
**Post date:** [April 15, 2020, 2:54pm UTC](https://www.ruby-forum.com/t/convert-a-array-of-string/259252/1 "2020-04-15T14:54:23Z")

</div>

I have a array of strings [“U1,U2”, “U3,U4”, “U2,U1”, “U1,U5”]

I want to remove the duplicate pairs

and also sort on the basis of their 0th index in alphabetical order

like [“U1,U2”, “U1,U5”,“U3,U4”]

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [April 15, 2020, 7:27pm UTC](https://www.ruby-forum.com/t/convert-a-array-of-string/259252/2 "2020-04-15T19:27:53Z")

</div>

`uniq` will remove duplicates from an array, and `sort` will sort it into order.  
[https://docs.ruby-lang.org/en/2.7.0/Array.html#method-i-uniq](https://docs.ruby-lang.org/en/2.7.0/Array.html#method-i-uniq)  
[https://docs.ruby-lang.org/en/2.7.0/Array.html#method-i-sort](https://docs.ruby-lang.org/en/2.7.0/Array.html#method-i-sort)

```auto
2.7.0 :002 > [1,4,3,3,2,1,4].uniq.sort
 => [1, 2, 3, 4]

```

But first you have to decide how you are treating your strings → should these also be arrays which you must sort before comparing?

---

<div class="post-metadata">

**Author:** ![jgomo3](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/jgomo3/32/950_2.png) [@jgomo3](https://www.ruby-forum.com/u/jgomo3)\
**Post date:** [April 20, 2020, 7:24pm UTC](https://www.ruby-forum.com/t/convert-a-array-of-string/259252/4 "2020-04-20T19:24:07Z")

</div>

It could be done “quick” by sorting each element first and then by calling `uniq` on the collection of sorted pairs, as @pcl told you.

But I suggest you to work with a better data structure than a string to represent the pair “U1,U2”.

One data structure that doesn’t care about the order of the elements is the `set`. So I suggest to get first an array or collection of sets.

Then `uniq` will remove the repeated sets.  
Or you can create a set from that data, that would result in the “same”.

Lastly, reconstruct your strings.

This would be my processing:

```ruby
["U1,U2", "U3,U4", "U2,U1", "U1,U5"]
  .map { _1.split(',')} # -> [["U1", "U2"], ["U3", "U4"], ["U2", "U1"], ["U1", "U5"]]
  .map { Set.new(_1) } # -> [#<Set: {"U1", "U2"}>, #<Set: {"U3", "U4"}>, #<Set: {"U2", "U1"}>, #<Set: {"U1", "U5"}>]
  .then { Set.new(_1) } # -> #<Set: {#<Set: {"U1", "U2"}>, #<Set: {"U3", "U4"}>, #<Set: {"U1", "U5"}>}>
  .map(&:sort) # -> [["U1", "U2"], ["U3", "U4"], ["U1", "U5"]]
  .sort # -> [["U1", "U2"], ["U1", "U5"], ["U3", "U4"]]
  .map { _1.join(',') } # -> ["U1,U2", "U3,U4", "U1,U5"]

```

Notice how only the first and last lines use those strings “U1,U2”, and the rest of the lines uses Tuples and Sets. You can thing on the first and last line as the interface of this computation, and the rest as the internals. Onions everywhere!!!
