galbe-droid
galbe-droid

Reputation: 9

Array won't sort for some reason

I'm testing a code to sort array in a custom alphabetic order, but for some reason each time i run the program the ordem don't sort

Main Code

using System;
using System.Collections.Generic;

class MainClass {
  public static void Main (string[] args) {
    string[] myArray = {"bbjcsnmh" , "kkr"};

    Array.Sort(myArray, MySorter.CompareStrings);

    foreach(string s in myArray)
    {
      Console.WriteLine(s); 
    }
  }
}

Custom Sorter

using System;
using System.Collections;

class MySorter
{
  public static int CompareStrings(string a, string b)
  {
    var newAlphabetOrder = "kbwrqdnfxjmlvhtcgzps";
    if(newAlphabetOrder.IndexOf((string) a) < newAlphabetOrder.IndexOf((string) b))
      return -1;

    return newAlphabetOrder.IndexOf((string) a) > 
    newAlphabetOrder.IndexOf((string) b) ? 1 : 0;
  }
}

Upvotes: 0

Views: 70

Answers (3)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

You have to compare corresponding characters of each string:

public static int CompareStrings(string a, string b) {
  if (ReferenceEquals(a, b)) // a and b are same references, e.g. both are null 
    return 0;
  if (null == a) // let null be smaller then any not-null string 
    return -1;
  if (null == b)
    return 1;

  var newAlphabetOrder = "kbwrqdnfxjmlvhtcgzps";

  for (int i = 0; i < Math.Min(a.Length, b.Length); ++i) {
    int indexA = newAlphabetOrder(a[i]);
    int indexB = newAlphabetOrder(b[i]);

    //TODO: you may want to add some logic if a[i] or b[i] is not in alphabet
    int compare = indexA.CompareTo(indexB);

    if (compare != 0)
      return compare; 
  }  

  // in case of same prefix, e.g. "kbw" and "kbwjjj" shorter string is smaller
  return a.Length.CompareTo(b.Length); 
}

Upvotes: 1

galbe-droid
galbe-droid

Reputation: 9

Thanks to Fildor, I manage to see where i was wrong, in this new sorter i change the ((char) a) to "a[0]" so it will check for the first letter of all interations of the array.

using System;
using System.Collections;

class MySorter
{
  public static int CompareStrings(string a, string b)
  {
    var newAlphabetOrder= "kbwrqdnfxjmlvhtcgzps";
    if(newAlphabetOrder.IndexOf(a[0]) < newAlphabetOrder.IndexOf(b[0]))
      return -1;

    return newAlphabetOrder.IndexOf(a[0]) > 
    newAlphabetOrder.IndexOf(b[0]) ? 1 : 0;
  }
}

Upvotes: 0

Athanasios Kataras
Athanasios Kataras

Reputation: 26450

Maybe you want to cange it like this:

using System;
using System.Collections;

class MySorter
{
  public static int CompareStrings(string a, string b)
  {
    var newAlphabetOrder = "kbwrqdnfxjmlvhtcgzps";
    int maxIterations = a.Length > b.Length ? b.Length : a.Length;
    for (int i = 0; i < maxIterations; i++) {
        if(newAlphabetOrder.IndexOf(a[i]) < newAlphabetOrder.IndexOf(b[i]))
             return -1;
        if(newAlphabetOrder.IndexOf(a[i]) > newAlphabetOrder.IndexOf(b[i]))
             return 1;
    }

    return 0;
  }
}

Net Fiddle - PoC

Upvotes: 1

Related Questions