Home > Software design >  Below is java script code, how can this be implemented in dart, it seems like < operator does
Below is java script code, how can this be implemented in dart, it seems like < operator does

Time:04-26

var user1 = "Frank"; // UID of user 1 var user2 = "Eusthace"; // UID of user 2

var roomName = 'chat_' (user1<user2 ? user1 '' user2 : user2 '' user1);

console.log(user1 ', ' user2 ' => ' roomName);

user1 = "Eusthace"; user2 = "Frank";

var roomName = 'chat_' (user1<user2 ? user1 '' user2 : user2 '' user1);

console.log(user1 ', ' user2 ' => ' roomName);

CodePudding user response:

var roomName = 'chat_' (user1<user2 ? user1 '' user2 : user2 '' user1);

could be translated to

var roomName = 'chat${user1.compareTo(user2) < 0 ? '$user1$user2' : '$user2$user1'}';

It could be that I am wrong with the <... If yes, > is correct. :-)

Okay, short explanation: x.compareTo(y) is a sorting method on many classes (including String). It returns -1 if x should be sorted before y, 1 if it should be sorted after y, and 0 if both are usually ranked equally.

Strings are sorted alphabetically (not sure about special characters like German umlauts though), DateTime objects are sorted as "earlier before later", and so on. In your own classes, you are free to define your own sorting logic (and it makes sense to also use a compareTo method for it).

  • Related