Skip to main content
  • Based on the naming guidelinesnaming guidelines parameters and local variables should be named using camelCase casing.
  • Because an ObservableCollection will by its nature contain multiple items, a parameter representing one should be named using the plural form.

An extract from a good readinggood reading about this subject

  • Based on the naming guidelines parameters and local variables should be named using camelCase casing.
  • Because an ObservableCollection will by its nature contain multiple items, a parameter representing one should be named using the plural form.

An extract from a good reading about this subject

  • Based on the naming guidelines parameters and local variables should be named using camelCase casing.
  • Because an ObservableCollection will by its nature contain multiple items, a parameter representing one should be named using the plural form.

An extract from a good reading about this subject

added 6 characters in body
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177
static void StableSort(ref ObservableCollection<string> values, ref ObservableCollection<int> weights)
{
    if (values == null) { throw new ArgumentNullException("values"); }
    if (weights == null) { throw new ArgumentNullException("weights"); }
        
    if (values.Count != weights.Count) { throw new ArgumentOutOfRangeException("collections count not equal", (Exception)null); }

    ObservableCollection<string>IList<string> localValues = new ObservableCollection<string>List<string>(values);
    ObservableCollection<int>IList<int> localWeights = new ObservableCollection<int>List<int>(weights);

    for (int i = 0; i < values.Count; i++)
    {
        int largestWeight = -1;
        int largestWeightIndex = 0;

        for (int j = i; j < values.Count; j++)
        {
            if (localWeights[j] > largestWeight)
            {
                largestWeight = localWeights[j];
                largestWeightIndex = j;
            }
        }

        if (largestWeightIndex == i) { continue; }

        localWeights.Insert(i, localWeights[largestWeightIndex]);
        localWeights.RemoveAt(largestWeightIndex + 1);

        localValues.Insert(i, localValues[largestWeightIndex]);
        localValues.RemoveAt(largestWeightIndex + 1);
    }

    values = localValues;new ObservableCollection<string>(localValues);
    weights = localWeights;new ObservableCollection<int>(localWeights);

}  
static void StableSort(ref ObservableCollection<string> values, ref ObservableCollection<int> weights)
{
    if (values == null) { throw new ArgumentNullException("values"); }
    if (weights == null) { throw new ArgumentNullException("weights"); }

    if (values.Count != weights.Count) { throw new ArgumentOutOfRangeException("collections count not equal", (Exception)null); }

    ObservableCollection<string>IList<string> localValues = new ObservableCollection<string>List<string>();
    ObservableCollection<int>IList<int> localWeights = new ObservableCollection<int>List<int>();

    int index = -1;
    var weightsWithIndex = weights.Select(p => new { Value = p, Index = ++index }).OrderByDescending(p => p.Value);

    foreach (var w in weightsWithIndex)
    {
        localWeights.Add(w.Value);
        localValues.Add(values[w.Index]);
    }

    values = localValues;new ObservableCollection<string>(localValues);
    weights = localWeights;new ObservableCollection<int>(localWeights);
}
static void StableSort(ref ObservableCollection<string> values, ref ObservableCollection<int> weights)
{
    if (values == null) { throw new ArgumentNullException("values"); }
    if (weights == null) { throw new ArgumentNullException("weights"); }
        
    if (values.Count != weights.Count) { throw new ArgumentOutOfRangeException("collections count not equal", (Exception)null); }

    ObservableCollection<string> localValues = new ObservableCollection<string>(values);
    ObservableCollection<int> localWeights = new ObservableCollection<int>(weights);

    for (int i = 0; i < values.Count; i++)
    {
        int largestWeight = -1;
        int largestWeightIndex = 0;

        for (int j = i; j < values.Count; j++)
        {
            if (localWeights[j] > largestWeight)
            {
                largestWeight = localWeights[j];
                largestWeightIndex = j;
            }
        }

        if (largestWeightIndex == i) { continue; }

        localWeights.Insert(i, localWeights[largestWeightIndex]);
        localWeights.RemoveAt(largestWeightIndex + 1);

        localValues.Insert(i, localValues[largestWeightIndex]);
        localValues.RemoveAt(largestWeightIndex + 1);
    }

    values = localValues;
    weights = localWeights;

}  
static void StableSort(ref ObservableCollection<string> values, ref ObservableCollection<int> weights)
{
    if (values == null) { throw new ArgumentNullException("values"); }
    if (weights == null) { throw new ArgumentNullException("weights"); }

    if (values.Count != weights.Count) { throw new ArgumentOutOfRangeException("collections count not equal", (Exception)null); }

    ObservableCollection<string> localValues = new ObservableCollection<string>();
    ObservableCollection<int> localWeights = new ObservableCollection<int>();

    int index = -1;
    var weightsWithIndex = weights.Select(p => new { Value = p, Index = ++index }).OrderByDescending(p => p.Value);

    foreach (var w in weightsWithIndex)
    {
        localWeights.Add(w.Value);
        localValues.Add(values[w.Index]);
    }

    values = localValues;
    weights = localWeights;
}
static void StableSort(ref ObservableCollection<string> values, ref ObservableCollection<int> weights)
{
    if (values == null) { throw new ArgumentNullException("values"); }
    if (weights == null) { throw new ArgumentNullException("weights"); }
        
    if (values.Count != weights.Count) { throw new ArgumentOutOfRangeException("collections count not equal", (Exception)null); }

    IList<string> localValues = new List<string>(values);
    IList<int> localWeights = new List<int>(weights);

    for (int i = 0; i < values.Count; i++)
    {
        int largestWeight = -1;
        int largestWeightIndex = 0;

        for (int j = i; j < values.Count; j++)
        {
            if (localWeights[j] > largestWeight)
            {
                largestWeight = localWeights[j];
                largestWeightIndex = j;
            }
        }

        if (largestWeightIndex == i) { continue; }

        localWeights.Insert(i, localWeights[largestWeightIndex]);
        localWeights.RemoveAt(largestWeightIndex + 1);

        localValues.Insert(i, localValues[largestWeightIndex]);
        localValues.RemoveAt(largestWeightIndex + 1);
    }

    values = new ObservableCollection<string>(localValues);
    weights = new ObservableCollection<int>(localWeights);

}  
static void StableSort(ref ObservableCollection<string> values, ref ObservableCollection<int> weights)
{
    if (values == null) { throw new ArgumentNullException("values"); }
    if (weights == null) { throw new ArgumentNullException("weights"); }

    if (values.Count != weights.Count) { throw new ArgumentOutOfRangeException("collections count not equal", (Exception)null); }

    IList<string> localValues = new List<string>();
    IList<int> localWeights = new List<int>();

    int index = -1;
    var weightsWithIndex = weights.Select(p => new { Value = p, Index = ++index }).OrderByDescending(p => p.Value);

    foreach (var w in weightsWithIndex)
    {
        localWeights.Add(w.Value);
        localValues.Add(values[w.Index]);
    }

    values = new ObservableCollection<string>(localValues);
    weights = new ObservableCollection<int>(localWeights);
}
added 1475 characters in body
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

So, what is this fancy, cool stuff

weights.Select(p => new { Value = p, Index = ++index })

??

We create for each item in weights a anonymous type where we create a property Value and assign the item of weights and before we assign the index to the Index property we increment it. So basically this is just like creating a Tuple but a way cooler.

Some related information

  • by definition anonymous types are immutable.

  • the order of properties matters

      var p1 = new { X=1, Y=2 };
      var p2 = new { X=1, Y=2 };
      var p3 = new { Y=2, X=1 };  
    

here p1.Equals(p2) == true but p1.Equals(p3) == false

An extract from a good reading about this subject

In short, an anonymous type is a reference type that derives directly from object and is defined by its set of properties base on their names, number, types, and order given at initialization. In addition to just holding these properties, it is also given appropriate overridden implementations for Equals() and GetHashCode() that take into account all of the properties to correctly perform property comparisons and hashing. Also overridden is an implementation of ToString() which makes it easy to display the contents of an anonymous type instance in a fairly concise manner.

So, what is this fancy, cool stuff

weights.Select(p => new { Value = p, Index = ++index })

??

We create for each item in weights a anonymous type where we create a property Value and assign the item of weights and before we assign the index to the Index property we increment it. So basically this is just like creating a Tuple but a way cooler.

Some related information

  • by definition anonymous types are immutable.

  • the order of properties matters

      var p1 = new { X=1, Y=2 };
      var p2 = new { X=1, Y=2 };
      var p3 = new { Y=2, X=1 };  
    

here p1.Equals(p2) == true but p1.Equals(p3) == false

An extract from a good reading about this subject

In short, an anonymous type is a reference type that derives directly from object and is defined by its set of properties base on their names, number, types, and order given at initialization. In addition to just holding these properties, it is also given appropriate overridden implementations for Equals() and GetHashCode() that take into account all of the properties to correctly perform property comparisons and hashing. Also overridden is an implementation of ToString() which makes it easy to display the contents of an anonymous type instance in a fairly concise manner.

Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177
Loading