Sort chained list in MVC standard project

Asked

Viewed 103 times

2

In my project I am using my own chain list implementation, that is, I am not using the ready list of the Java API. In addition, my implementation is of a generic list, which has Object objects on its nodes, so that it can be used for any type of Object. I need to sort this chained list according to a certain attribute of a specific type of object. In the MVC standard, what was the ideal place for this ordering? I thought to perform the ordination on the chained list itself, but this would "break" the generality of it, for the ordination I wish is only for a specific type of object.

  • 4

    As the question was quickly closed (something that has happened a lot lately) I will try to answer by comment. State in your implementation a method called, for example sort(), who receives a Comparator<T> and use it to sort the list. The consumer in your class, when you want to do the Sort, defines the Comparator for your specific case and passes it to the method sort(). In this way the implementation of the chain-linked list would remain generic.

  • 1

    Vinicius, I think in the case of MVC you’re going a little too far in the concepts. First, MVC will not dictate anywhere how you implement your data structures and other implementation details. MVC sets only the default for some types of classes, more specifically three types. There is no general best practice that will tell you at what time or how you should sort a list. It depends on the rules of the system, for example, whether that list is sorted only at a time or whether it should always be sorted.

  • 1

    Besides, you said you used Object to accept any type of object, but using Generics you could have done the same without using Object directly and maintaining type coherence, just as it is done in the Java API. But there are still some questions: does this mean that the same instance of the list should accept objects of different types? What should happen if you have in this list a mix of items including this specific object containing said attribute and other different objects?

  • 1

    If the list for this type of object needs to be sorted always, but not other types, then you are in need of another type of list. You can use inheritance and extend the original list or create a new polymorphic implementation from an interface.

  • 1

    But it would be really ideal if you post your code, because I’m sure several people would like to help with this problem. :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.