Hi !
First it seems that the expected output is not the one which respects your rules.
"There are more words in the world than worlds in the word" should be first since it contains two duplicates : "
in" and "
the".
The expected output is now :
There are more words in the world than worlds in the word.
Hello hello beautiful world!
This is a sentence.
My name is Neo.
Hello World.
After here is my suggestion :
What about creating a class with a custom
Sort()
method.
It's not possible to override
Sort()
method of the String class since this class is sealed.
So we create a class only to encapsulate your
String
and create your own
Sort()
method.
Lets call this class
CustomString
.
In order to be able to use the
Sort()
method, you have to implement the
IComparable
interface. In order to implement this interface, all you have to do is to create the method
CompareTo
().
CompareTo
indicates how two elements should be compared. The good point is that you know your comparison elements :
- Duplicate words
- Number of words
- Number of characters
See here how to implement the inteface :
Sorting Lists using IComparable and IComparer Interface in .NET
And after, you just have to use
Sort()
on your List of
CustomString
.
Since you seems to struggle a bit and be afraid by my solution, i must show you how simple it is.
80% of the code is your.
Solution :
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace CustomCompare
{
internal class Program
{
private static void Main(string[] args)
{
var text = File.ReadAllText("Your File");
var sentences = Regex.Split(text, @"(?<=[\.!\?])\s");
var sortedList = sentences.Select(sentence => new CustomString(sentence)).ToList();
sortedList.Sort();
foreach (var customString in sortedList)
{
Console.WriteLine(customString.CustString);
}
Console.Read();
}
}
internal class CustomString : IComparable<CustomString>
{
public CustomString(string custString)
{
CustString = custString;
}
public string CustString { get; set; }
public int CompareTo(CustomString other)
{
var thisNumberOfDuplicate = DublicatesCounter(CustString);
var otherNumberOfDuplicate = DublicatesCounter(other.CustString);
if (thisNumberOfDuplicate > otherNumberOfDuplicate)
return -1;
if (thisNumberOfDuplicate < otherNumberOfDuplicate)
return 1;
var thisNumberOfWords = WordCounter(CustString);
var otherNumberOfWords = WordCounter(other.CustString);
if (thisNumberOfWords > otherNumberOfWords)
return -1;
if (thisNumberOfWords > otherNumberOfWords)
return 1;
var thisNumberOfCharacter = LetterCounter(CustString);
var otherNumberOfCharacter = LetterCounter(other.CustString);
if (thisNumberOfCharacter > otherNumberOfCharacter)
return -1;
if (thisNumberOfCharacter < otherNumberOfCharacter)
return 1;
return 0;
}
private int WordCounter(string sentence)
{
var wordsCount = 0;
wordsCount = sentence
.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries)
.Count();
return wordsCount;
}
private int LetterCounter(string sentence)
{
return sentence.Length;
}
private int DublicatesCounter(string sentence)
{
var numberOfDuplicate = 0;
var splited = sentence.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries);
var isChecked = new bool[splited.Length];
for (var j = 0; j < splited.Length - 1; j++)
{
var currentWord = splited[j];
for (var k = j + 1; k < splited.Length; k++)
{
if (isChecked[k])
{
continue;
}
if (currentWord.ToLower() == splited[k].ToLower())
{
numberOfDuplicate++;
isChecked[k] = true;
}
}
}
return numberOfDuplicate;
}
}
}
Output:
There are more words in the world than worlds in the word.
Hello hello beautiful world!
This is a sentence.
My name is Neo.
Hello World.