Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

userCompare  challenge

Posted on 2016-08-30
3
Medium Priority
?
101 Views
Last Modified: 2016-09-02
Hi,


i am working one below challenge
http://codingbat.com/prob/p143482

i was not clear on below description

We have data for two users, A and B, each with a String name and an int id. The goal is to order the users such as for sorting. Return -1 if A comes before B, 1 if A comes after B, and 0 if they are the same. Order first by the string names, and then by the id numbers if the names are the same. Note: with Strings str1.compareTo(str2) returns an int value which is negative/0/positive to indicate how str1 is ordered to str2 (the value is not limited to -1/0/1). (On the AP, there would be two User objects, but here the code simply takes the two strings and two ints directly. The code logic is the same.)
what is A and what is B here to sort??

how below results are -1 and 1 and 0 please advise
userCompare("bb", 1, "zz", 2) → -1
userCompare("bb", 1, "aa", 2) → 1
userCompare("bb", 1, "bb", 1) → 0
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Accepted Solution

by:
Bill Bach earned 1000 total points
ID: 41778354
You really need to read the entire description carefully.  If you are going to be a coder, you must have a mind for details.

>what is A and what is B here to sort??
If you look at the code window on that page, you will see:
   public int userCompare(String aName, int aId, String bName, int bId)
The first two fields are the string and integer for A, and the second two fields are the string and integer for B.  You are not sorting them, but only comparing them.

>how below results are -1 and 1 and 0 please advise
In the first example, the string for A is less than the string for B, so it returns -1.
In the second example, the string for A is more than the string for B, so it returns 1.
In the third example, the string for A is the same as the string for B, so it then compares the integer fields.  The integer fields are the same, so it returns 0.

This is stated in the problem just as clearly:
Return -1 if A comes before B,
[return] 1 if A comes after B, and
[return] 0 if they are the same.
Order first by the string names, and then by the id numbers if the names are the same.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 1000 total points
ID: 41779284
public int userCompare(String aName, int aId, String bName, int bId) {
  return (int)Math.signum(2*Math.signum(aName.compareTo(bName))+Math.signum(aId-bId));
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 41782484
public int userCompare(String aName, int aId, String bName, int bId) {
  int res=aName.compareTo(bName);
  if(aName.equals(bName)&&(aId==bId)){
    return 0;
  }
  if(res>0){
    return 1;
    
  }else if(res<0){
    return -1;
    
  }else if(aId>bId){
    return 1;
    
  }else 
    return -1;
    
  
  
}

Open in new window


above also passed all tests. Any improvements or alternate approaches?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question