#Object Identifier as a variable
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If your code is long, or you have multiple files to share, consider posting it on sites like https://pastebin.com/ and share the link instead, that is easier to browse for helpers.
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Ooooh okay so this is a really good question.
Have you ever heard of reflection?
It's a pretty advanced Java feature.
But that's what reflection was made for.
well thanks a lot, now i know at least, with what tools to work
Or perhaps, if you share some more of your code I can suggest an alternative approach. Reflection should only be used where it's needed, not where it can be used.
okay i gotta explain a little bit
so this is my object with these properties
which i end up saving here
Okay, so why do you need to access a property with an arbitrary name instead of creating getters for each field?
now i display this in a table and i want to sort these for each property
i wanted to create a method that sorts all the strings, as the name, last name, etc and a method to sort all integer
You should definitely not use reflection here.
so you want to sort the items by arbitrary fields or ?
and as the sort criterium should be my String a == the selected property
how do you get this string a ?
do you decide at some point to sort in a way
or is it totally arbitrary
select one of the these
ah so it is arbitrary
and then press a button or so
wait no
this is by name right ?
so you only want to sort by name ?
yeah
i want to be able to sort the table for each of them
either you do a case for each
it's a single line by field
or you use reflection
already done to identify what property i want to acces (ID in the combocox is int id; in the properties) but then i still gotta write for each attribute a sort algorithm
also is this supposed to sort ?
no
sort is a thing that already exist
customers.sort(Comparator.comparing(Customer::getName))
Assuming customers is a List<Customer> and Customer has a method getName, this will sort the list by name
if List means ArrayList ... :/ sorry i ask
yes
ok... and i can do that with every property no matter what data type?
so with String and int?
As long as the type implements Comparable, it's fine
and for int or other primitive, you would use for example
customers.sort(Comparator.comparingInt(Customer::getAge))
ok some last questions
it is right that its :getAge not :getAge() ? i thought methods always with brackets
yes
that's called a method reference
do you know interfaces and inheritance ?
ok sorry for my bad skills :/
not yet
right
so this is a bit advanced
but basically
sort method wants a Compaartor
so you just need to give it a comparator
and the value from :getAge is just a method that... gives back the age, so the property i want to sort...
a comparator is an object that has a single method, which takes two objects and returns a negative, 0 or positive int depending if the first is smaller, equal or bigger than the second
so you could make your own comparator
but there is also a neat method which make it a bit easier : Comparator.comparing
this method takes a method reference and will use it as a key to create the comparator
ooookokok didnt understand every word however the sense
for example, if I use the method reference getName, then it will use getName on the first and second object in the comparator to find which one is greater
gonna try to implement it later, thanks a lot :)
class CustomerNameComparator implements Comparator<Customer> {
@Override
public int compare(Customer a, Customer b){
return a.getName().compareTo(b.getName());
}
}
this is a comparator for example
and it will compare the name of two customers
so you could do
customers.sort(new CustomerNameComparator());
this will work
do you understand this code @cold sigil ?
i guess you mean getAge right?
no it compares by the name here 🤔
oh nvm you changed sorry i read age
so you may notice that in this comparator, if you want to change it with for example lastName, or whatever, you only need to change getName method right ?
yesyesyes i understand omg
class CustomerLastNameComparator implements Comparator<Customer> {
@Override
public int compare(Customer a, Customer b){
return a.getLastName().compareTo(b.getLastName());
}
}
so what Comparator.comparing does
is that it takes a reference to the method you want to use
and build this whole class for you
thanks for your help, i gotta leave, but answere later <3
(I am oversimplificating here but it's basically that)
still thank you, i'm gonna try it out
@radiant dragon thank you so much, it works!!!!!! :)