I made a C# List containing 79244 words from a text file. There is an InputField and a Submit Button. When I type a word and press the Submit button, using the BinarySearch() function of the List class, the code shall display the index of that word in the list. Whenever, a word is found, the BinarySearch() function should return its positive index value, but in this case, it is always returning a negative number. Have I done something wrong? Please help.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CheckDict : MonoBehaviour {
public InputField inpText;
public Text showText;
public TextAsset dictFile;
private string textToString;
private List<string> dictList;
private int dictLen;
void Start () {
dictList = TextToList (dictFile);
dictLen = dictList.Count;
showText.text = dictLen.ToString();
}
// function to extract each word in the textfile separated by a new line and adding it to the string
private List<string> TextToList (TextAsset ts) {
return new List<string> (ts.text.Split ('
'));
}
public void OnSubmit () {
textToString = inpText.text.ToString();
int index = dictList.BinarySearch (textToString);
Debug.Log (index);
// if (dictList.BinarySearch (textToString) < 0)
// showText.text = "Word missing from dictionary";
// else
// showText.text = textToString;
}
}
Output: