Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
We can also sort alphabetically.
The goal of this lesson is to sort by class name and price.
Open up the folder for this lesson by following this path inside the resources.
The string class also implements comparable.
It follows that the string class overrides compared to.
The compared to method for string objects returns a negative number if the character in the first string
has a lower Unicode value than the character in the specified object.
Zero, if the strings are identical, which makes sense.
And a positive number, if a character in the first string has a higher Unicode value than a character
in the second string.
We'll test the first case first, we have John and Amy, we'll call Amy compared to.
John.
The letter A. is expected to have a lower Unicode value than the letter J.
So that's going to return a negative number.
Perfect.
Now, I'll compare Amy to Amy.
Returns zero expected.
Now, I'm going to compare John against Jane.
JNJ, same Unicode value, but the letter O comes after a so we can expect a higher Unicode value perfect.
And if we reverse it, we get a negative number.
So based on this information that you just got, go back to the product application.
And what I want you to do is sort the products by alphabetical order based on their class type, then
sort the rest by price.
Here are some hints you can get the class type by using get class and you can get the name of the class
by using get class, get simple name.
I'll give you the chance to pause the video and try this out.
I hope you figured it out.
So what I'm going to do is inside compared to I'll get the name of the current object.
String class name.
Is equal to this doGet class, that simple name.
And I'll get the last name of the specified object.
All right, now we're going to check if the class names aren't equal.
If that's the case, we're going to return the result from comparing the first class name to the second
one.
Here we're comparing two strings, and if the two objects don't happen to share the same class, so,
for example, if this is pants and this is shirt compared to is going to return an integer, that depends
on the letters in each name.
Let's test this out by running the debugger.
These two objects don't share the same class.
This is Pence, this is Schertz, the letter s is expected to have a higher Unicode value than the letter
P.
So this is going to return a positive value, which means shirt is going to come after pence.
Now we're comparing pants and pants, the two pants are part of the same class, however, the price
of the current object is higher than the price of the specified object.
So compared to returns, a positive number.
So it's going to sort the current object that come after the specified one.
And so basically it's going to keep comparing each object against another object.
First, it prioritizes alphabetical order.
And if the two objects share the same class, then it's going to sort of by comparing the price of each
product.
Ultimately, we get an array where objects of different classes are sorted based on the class name,
and every object of the same class are compared by price.
The sort method can easily sort to comparable objects, sort uses compare to to determine how to sort
your objects.
And if the two objects don't have the same class, the one with characters that have a lower Unicode
value are going to be sorted first.
If the names happen to be equal, then the objects are compared based on their price.
As a result, our comparable objects are sorted based on alphabetical order and price.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.