• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Comparators, 2D Arrays

 
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Let's say I want a Comparator that sorts the longest strings first and there are no nulls values.



I am told this is the correct method for doing so, but doesn't this method work as well?:



Both methods return a number that signifies what string is greater, so isn't this enough information to sort?

2.
In a 2D array, letters does "letters[0]" refer to the 1st row?
 
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dylan Kwon wrote:1. Let's say I want a Comparator that sorts the longest strings first and there are no nulls values.



I am told this is the correct method for doing so, but doesn't this method work as well?:



Well, it is a bit confusing how it is asked, but the problem basically is to sort strings based on their lenghts in either:
a. ascending order (shortest to longest)
b. descending order (longest to shortest)

First variant would achieve (a), while second variant would achieve (b). So that's the difference between the two implementations.

Why is like so? Look at Comparator interface documentation. It says:

Parameters:
o1 - the first object to be compared.
o2 - the second object to be compared.
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.


So let's say first string s1 (or o1 as it's called in docs) is of length 5 and the second string s2 is of length 10. Obviously there is a difference whether you subtract 10 from 5 or 5 from 10. You get either -5 or +5 respectively.

Based on explanation in Returns part, it is explained, how depending on the expression result is defined which element is smaller, greater or they both are equal.


Dylan Kwon wrote:2.
In a 2D array, letters does "letters[0]" refer to the 1st row?


People normally think about rows and columns when they trying to visualise an array of arrays. But really there is nothing about rows or columns.


So you know, it is comfortable to think as about rows and columns when you speak with someone, where i.e. A[0] is a first row, and A[1][1] would refer to a number 5, which is row 2 and column 2, but that's only because I formatted this array of arrays that way. I could have just formatted it simply that way:

Now we have what? Just a one row from its visual looks.

So it is an array of arrays. A[0] is referring to a very first array within an array. It could be more complicated, an array of arrays, that each array's element is an another array, then another... How you'd represent rows then? Probably that would be representation with more dimensions, but again, that's just model how you visualise in your head.
 
Dylan Kwon
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for clarifying!

For the Comparators, wouldn't both methods work for both scenarios? From my understanding, sort algorithms use compare methods to put values in order. Since both compare methods tell you if o1 is less than, equal to or greater than o2, why are they each used for 1 scenario?
 
Bartender
Posts: 5567
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say String s1 has length 5, and String s2 has length 10. In your first Comparator you return s2.length() - s1.length() = 5. Since that is positive, the first argument s1 is considered to be the bigger one and so will come after s2 in the sort. Your second Comparator does the reverse: since 5 - 10 is negative, s1 is considered to be smaller.
 
Dylan Kwon
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Say String s1 has length 5, and String s2 has length 10. In your first Comparator you return s2.length() - s1.length() = 5. Since that is positive, the first argument s1 is considered to be the bigger one and so will come after s2 in the sort. Your second Comparator does the reverse: since 5 - 10 is negative, s1 is considered to be smaller.



I see, thank you. What if s1 had length 10 and s2 had length 5? Or do we assume that s1 will always have a smaller length?
 
Piet Souris
Bartender
Posts: 5567
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We assume nothing. The lengths of both Strings are unimportant. In the first Comparator, with compare(s1, s2) = s2.length() - s1.length, if s2 is longer than s1, the outcome is positive, and therefore the first argument, s1, is considered to be the bigger of the two. If s1 is longer than s2, the outcome is negative, and so s1 is smaller than s2. All the necessary comparisons for the sorting will be done in this way. And, as it should be: check that compare(s1, s2) = -compare(s2, s1).

An alternative way to define your Comparators is
 
Dylan Kwon
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, I understand fully now!
 
Hold that thought. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic