package ru.micord.ervu.account_applications.component.util;

import java.util.Arrays;

import model.grid.GridRow;
import model.grid.GridRows;
import ru.micord.ervu.account_applications.util.IpAddressUtils;

import ru.cg.webbpm.modules.database.api.dao.option.SortOrder;

/**
 * @author Adel Kalimullin
 */
public final class GridUtils {

  private GridUtils() {}

  public static void sortByIp(GridRows gridRows, SortOrder sortOrder, String columnName) {
    if (gridRows == null || gridRows.getRows() == null) {
      return;
    }

    GridRow[] rows = gridRows.getRows();

    Arrays.sort(rows, (row1, row2) -> {
      String ip1 = (String) row1.get(columnName);
      String ip2 = (String) row2.get(columnName);

      int comparison = IpAddressUtils.compareIpAddresses(ip1, ip2);

      return sortOrder == SortOrder.DESC ? -comparison : comparison;
    });
  }
}
