Here you can find the source of orderByFuzzyFirstOccurenceInText(String listToOrder[], String textToParse)
public static List<String> orderByFuzzyFirstOccurenceInText(String listToOrder[], String textToParse)
//package com.java2s; /*/* w w w .jav a2s . co m*/ * Copyright 2010 Research Studios Austria Forschungsgesellschaft mBH * * This file is part of easyrec. * * easyrec is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * easyrec is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with easyrec. If not, see <http://www.gnu.org/licenses/>. */ import java.util.*; public class Main { /** * This function sorts the Strings in the given list by their first * occurrence in the given text. The Strings in the list are tokenized * e.g. "red bull" is split in "red" and "bull" those tokens are * matched by their first occurrence in the text. */ public static List<String> orderByFuzzyFirstOccurenceInText(String listToOrder[], String textToParse) { List<String> sortedList = new ArrayList<String>(); HashMap<Integer, String> h = new HashMap<Integer, String>(); if (listToOrder != null && !textToParse.equals("")) { for (String stringTokens : listToOrder) { String[] tokens = stringTokens.split(" "); for (String token : tokens) { if (textToParse.indexOf(token) > 0 && token.length() > 3) { h.put(textToParse.indexOf(token), token); } } } List<Integer> keys = new ArrayList<Integer>(h.keySet()); Collections.sort(keys); for (Integer k : keys) { sortedList.add(h.get(k)); } } return sortedList; } }