Hi,

I'm trying to sort ip adresses which are entered in a text type field. The current outcome is this:

192.168.164.123
192.168.164.124
192.168.164.4
192.168.164.5
192.168.165.100
192.168.165.20

And I'd like to have it like this:
192.168.164.4
192.168.164.5
192.168.164.123
192.168.164.124
192.168.165.20
192.168.165.100

Is there a way to archieve this or do I have to change the field type?

Thanks in advance!

Nda

Comments

dawehner’s picture

Status: Active » Fixed

Sure, write your own sort handler and use INET_ATON(field). This behaves exact like you want it.

Nda’s picture

Allright thanks dereine. I've been reading up abit on how to do such a thing and it looks like I need to create a module, name it "views_sort_ip" for example, create a .module file and a views_sort_ip_handler_field_something.inc. Then copy some code here and there. Only thing I can't seem to find is where or when I can see if what I'm doing is working yet but I'll try and see what happens.

I've never done anything remotely similar in Drupal yet so if you have some pointers or an example that'd be greatly appreciated.

Thanks!

Nda’s picture

Allright, I've come this far:

file: views_sort_ip.module

function views_sort_ip_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'views_sort_ip'),
  );
}

file: views_sort_ip.module

<?php
function views_sort_ip_views_data()
{
  $data['views_sort_ip']['table']['group'] = t('views_sort_ip');
  $data['views_sort_ip']['table']['join'] = array(
    '#global' => array(),
  );

  $data['views_sort_ip']['ip'] = array(
    'title' => t('Sorteren op ip adres'),
    'help' => t('IP adressen sorteren'),
    'sort' => array(
      'handler' => 'views_handler_sort_ip',
      ),
  );
  return $data;
}

function views_sort_ip_views_handlers()
{
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'views'),
    ),
    'handlers' => array(
    'views_handler_sort_ip' => array(
      'parent' => 'views_handler_filter_numeric',
       ),
    ),
  );
}

file: views_handler_sort_ip.inc

<?php

class views_handler_sort_ip extends views_handler sort {
  function query ()
    $this->ensure_my_table();
    $this->query->add_orderby($this->table_alias, $this->field . $i, $this->options['order']);

Now I don't know how to insert the INET_ATON option into the query. I've seen a few examples but all seemed too complicated for what I need and didn't give me an impression on how to get there.
The ip address is in the default node title field so in mysql it would be: "order by INET_ATON(title);" Besides this, I don't know what will happen when I activate the module. Will I get a new option next to sorting ascending or descending or will the field automatically be sorted this way?

Hope you can help.

Kind regards, Nda

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

kenorb’s picture

Title: Sorting ip adresses » Sorting IP addresses