List OrderBy Alphabetical Order
If you mean an in-place sort (i.e. the list is updated): people.Sort((x, y) => string.Compare(x.LastName, y.LastName)); If you mean a new list: var newList = people.OrderBy(x=>x.LastName).ToList(); // ToList optional
If you mean an in-place sort (i.e. the list is updated): people.Sort((x, y) => string.Compare(x.LastName, y.LastName)); If you mean a new list: var newList = people.OrderBy(x=>x.LastName).ToList(); // ToList optional
The issue with List<String> list = new LinkedList(); is that on the left hand side, you are using the generic type List<String> where on the right side you are using the raw type LinkedList. Raw types in Java effectively only exist for compatibility with pre-generics code and should never be used in new code unless … Read more
Sometimes Java generics just doesn’t let you do what you want to, and you need to effectively tell the compiler that what you’re doing really will be legal at execution time. I usually find this a pain when I’m mocking a generic interface, but there are other examples too. It’s usually worth trying to work … Read more
Method to deserialize generic collection: import java.lang.reflect.Type; import com.google.gson.reflect.TypeToken; … Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType(); List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType); Since several people in the comments have mentioned it, here’s an explanation of how the TypeToken class is being used. The construction new TypeToken<…>() {}.getType() captures a compile-time type (between the < and >) … Read more
Heap pollution is a technical term. It refers to references which have a type that is not a supertype of the object they point to. List<A> listOfAs = new ArrayList<>(); List<B> listOfBs = (List<B>)(Object)listOfAs; // points to a list of As This can lead to “unexplainable” ClassCastExceptions. // if the heap never gets polluted, this … Read more
Well, I can’t answer why it’s not available, but I can confirm that it’s not a CLI issue. The CLI spec doesn’t mention it (as far as I can see) and if you use IL directly you can create a generic attribute. The part of the C# 3 spec that bans it – section 10.1.4 … Read more
If you are exposing your class through a library that others will use, you generally want to expose it via interfaces rather than concrete implementations. This will help if you decide to change the implementation of your class later to use a different concrete class. In that case the users of your library won’t need … Read more
Iterate your list in reverse with a for loop: for (int i = safePendingList.Count – 1; i >= 0; i–) { // some code // safePendingList.RemoveAt(i); } Example: var list = new List<int>(Enumerable.Range(1, 10)); for (int i = list.Count – 1; i >= 0; i–) { if (list[i] > 5) list.RemoveAt(i); } list.ForEach(i => Console.WriteLine(i)); … Read more
As others mentioned, it’s only possible via reflection in certain circumstances. If you really need the type, this is the usual (type-safe) workaround pattern: public class GenericClass<T> { private final Class<T> type; public GenericClass(Class<T> type) { this.type = type; } public Class<T> getMyType() { return this.type; } }
If you’re using .Net 3+, you can use Linq. List<T> withDupes = LoadSomeData(); List<T> noDupes = withDupes.Distinct().ToList();