-2

I want to sort on multiple fields and have found the answer : How do I sort a list by different parameters at different timed which is what I was looking for.

I want to expand on it and pass a particular variable for specific enums. How can this be achieved? (the purpose of me passing a variable is so I can do a calculation and then sort based on that).

Community
  • 1
  • 1
user648244
  • 1,240
  • 7
  • 26
  • 41
  • 1
    enums can have member variables like other classes. But I don't understand what you're trying to achieve. What do you mean with "** - variableName**" ? – EasterBunnyBugSmasher May 08 '14 at 10:36
  • 1
    Please add some more details. As it stands, your question is unclear – Bohemian May 08 '14 at 10:37
  • 1
    It would defeat the purpose of having an enum, wouldn't it? – Maurice Perry May 08 '14 at 10:38
  • I want to pass a integer to the enum, so when a particular enum gets called i can do further processing during the sort – user648244 May 08 '14 at 10:40
  • If you need a parameterized comparator, then it can't be an enum, since you want to be able to create a specific comparator instance for every possible parameter value. Just create a ByIdWithParameterComparator class, taking a parameter as argument of its constructor. – JB Nizet May 08 '14 at 10:41

1 Answers1

1

No, it's not possible (well, technically it's not impossible, but it would be a huge kludge with reflection, and a very bad idea). If you have a separate Comparator for all your different sort options (provided that there are only a handful, maybe 5-10 cases), you have a nice strongly typed system in place.

Besides, if the name is ID_SORT, why would you have it sort by anything besides the ID? It would be confusing to anyone using the code.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • was just an example, i would do a different type of sort. just looking for a neat way of doing this – user648244 May 08 '14 at 10:40
  • I think you're looking for a way to do that without having to write code. The resulting solution (with reflection) wouldn't be neat, even though it would be shorter. – Kayaman May 08 '14 at 10:57