title: How can I implement binary search in Substring in function and two values in Comparator?
body: <p>I have a problem about implementing binary search in substring. In my City object, there is a cityName variable as defined String. I want to enter any substring like "Sha" and it shows the result of "Sha". Except for this, there is a weight variable to sort descend city names. For example, the bigger weight value is at the top and the sort process is based on descending.</p>
  
  <p>How can I do that? How can I add this to the <code>comparater</code> area?</p>
  
  <p>Here is my City Object </p>
  
  <pre><code>public class City implements Serializable{
      private Integer cityWeight;
      private String cityName;
  }
  </code></pre>
  
  <p>Here is my code snippet shown below.</p>
  
  <p>// Not implement Binary Search </p>
  
  <pre><code>public static Integer[] searchByCharacters(List&lt;City&gt; list, String sub) {
          List&lt;Integer&gt; result = new ArrayList&lt;&gt;();
          Collections.sort(list);
          for (int i = 0; i &lt; list.size(); i++) {
              if (list.get(i).getCityName().contains(sub))
                  result.add(i);
          }
          return result.stream().toArray(Integer[]::new);
  }
  </code></pre>
  
  <p>I want to implement binary search above the code snippet but it doesn't work.</p>
  
  <pre><code>public static Integer[] searchBySubStringCharacter(List&lt;City&gt; list, String sub) {
          List&lt;Integer&gt; result = new ArrayList&lt;&gt;();
  
          Comparator&lt;City&gt; comparator = new Comparator&lt;City&gt;() {
              public int compare(City node1, City node2) {
                  boolean node1Contains = node1.getCityName().contains(sub);
                  boolean node2Contains = node2.getCityName().contains(sub);
                  if (node1Contains &amp;&amp; !node2Contains) {
                      return 1;
                  } else if (!node1Contains &amp;&amp; node2Contains ) {
                      return -1;
                  } else {
                      return 0;
                  }
              }
          };
  
          Collections.sort(list, comparator);
          for (int i = 0; i &lt; list.size(); i++) {
              int index = Collections.binarySearch(list, new City(sub), comparator);
              if (index &gt;= 0)
                  result.add(i);
          }
  
          return result.stream().toArray(Integer[]::new);
      }
  </code></pre>
  
  <p>This code shows all city list and the result of binary search. How can I extract all city list from the result?</p>