1

I have some lists I want to order by a value and reverse them afterwards. Instead of ordering each list by their own values, both lists got ordered by the values of the second one.

List<Sector> tempSectors = Lists.LSectors;
List<Sector> orderedByCTW = new List<Sector>();
List<Sector> orderedByCTM = new List<Sector>();

orderedByCTW = tempSectors;
orderedByCTM = tempSectors;

orderedByCTW.Sort((s1, s2) => s1.CLTW.Count.CompareTo(s2.CLTW.Count));
orderedByCTM.Sort((s1, s2) => s1.CLTM.Count.CompareTo(s2.CLTM.Count));
orderedByCTW.Reverse();
orderedByCTM.Reverse();
Utility.MostValueableSectorTW = orderedByCTW.FirstOrDefault();
Utility.MostValueableSectorTM = orderedByCTM.FirstOrDefault();

orderedByCTW is also ordered by the values of CTM, but why?

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Michael
  • 39
  • 6
  • Possible duplicate of [How do I change my new list without changing the original list?](https://stackoverflow.com/questions/13977841/how-do-i-change-my-new-list-without-changing-the-original-list) – mjwills Jun 12 '18 at 01:08
  • [What is the difference between a reference type and value type](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c). -- [Value and Reference types confusion](https://stackoverflow.com/questions/45589783/value-and-reference-types-confusion?rq=1). – Jimi Jun 12 '18 at 01:25

1 Answers1

0

You're assigning tempSectors to both of your other variables, orderedByCTM and orderedByCTW.

So it's actually the same list. You sort it one way, then another. Then you reverse it, and reverse it again.

Instead, perhaps make a copy.

orderedByCTW = tempSectors.ToList();
orderedByCTM = tempSectors.ToList();

Now you've got copies of the same original list, instead of just the original list.

Josh
  • 2,958
  • 3
  • 16
  • 27
  • 1
    Consider this. I'm going to make some assumptions that you can simply accept for this example. You have a mother and a father. Your mother has a husband. If you put a blue shirt on your father and then your mother puts a red shirt on her husband, would you be surprised to see a red shirt on your father? Of course, not, because your father and your mother's husband are not two different people. The same goes here. You have two variables, yes, but they are referring to the same object. Any changes made to the object via one variable will be reflected in the other variable in the same way. – jmcilhinney Jun 12 '18 at 01:08
  • ah i see.. i thought filling the both lists and work inside them will do the thing and dont affect the other one – Michael Jun 12 '18 at 01:19
  • `tempSectors` is a reference to the memory location where the list is stored. So when you assign `orderedByCTW = tempSectors;`, you now have a new reference to the same location. – Rufus L Jun 12 '18 at 01:24
  • so when im ordering orderedbyCTW im really ordering tempsectors and afterwards the same with orderedbyCTM affecting tempsectors.. right? – Michael Jun 12 '18 at 01:26